Skip to content

between and sepBy return unknown type #109

@devurandom

Description

@devurandom

Following code has type errors with TypeScript 5.1.3:

import {
    between,
    char,
    regex,
    sepBy,
    sequenceOf,
    whitespace
} from "arcsecond";

const word = regex(/^\w+/);

const quotedString = between(char('"'))(char('"'))(regex(/^[^"]+/));

const attribute = sequenceOf([word, char("="), quotedString]).map(
    ([name, _, value]): [string, string] => [name, value]
);

const attributeList = sepBy(whitespace)(attribute).map(
    (values): [string, string][] => values
);

export const parse = (input: string) => attributeList.run(input);
❯ pnpm build

> arcsecond-repro@0.1.0 build [REDACTED]/arcsecond-repro
> tsc -p .

src/parser.ts:15:52 - error TS2322: Type 'unknown' is not assignable to type 'string'.

15     ([name, _, value]): [string, string] => [name, value]
                                                      ~~~~~

src/parser.ts:19:37 - error TS2322: Type 'unknown[]' is not assignable to type '[string, string][]'.
  Type 'unknown' is not assignable to type '[string, string]'.

19     (values): [string, string][] => values
                                       ~~~~~~


Found 2 errors in the same file, starting at: src/parser.ts:15

 ELIFECYCLE  Command failed with exit code 2.

I would expect:

  • quotedString to contain type string, but TypeScript detects it as unknown.
    • Generally: I would expect between(_)(_)(T) to have type T instead of unknown.
  • attributeList to contain type [string, string][], but TypeScript detects it as unknown[].
    • Generally: I would expect sepBy(_)(T) to have type T[] instead of unknown[].

Please find a complete example in https://github.com/devurandom/arcsecond-issue-109-repro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions