Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript 4.7 A extends B<infer C extends D> fails with a syntax error #2330

Closed
IgnusG opened this issue Jun 18, 2022 · 1 comment
Closed

Comments

@IgnusG
Copy link

IgnusG commented Jun 18, 2022

This code seems to fail even with the latest version of esbuild and I'm not sure if I configured something wrong or if this syntax isn't supported by esbuild yet:

type JoinDot<Items extends string[], Accumulator extends string = ""> = Items extends [
	infer Item extends string,
	...infer Rest extends string[],
]
	? [] extends Rest
		? `${Accumulator}${Item}`
		: JoinDot<Rest, `${Accumulator}${Item}.`>
	: never;

This new TS 4.7 syntax simplifies the definitions slightly but esbuild seems to not like it. I could rewrite it to use the old syntax but it just adds a lot of nesting.

Error Stack
Error: Transform failed with 1 error:
/.../file.ts:45:26: ERROR: Expected "?" but found ","
    at failureErrorWithLog (/.../node_modules/esbuild/lib/main.js:1612:15)
    at /.../node_modules/esbuild/lib/main.js:1401:29
    at /.../node_modules/esbuild/lib/main.js:675:9
    at handleIncomingPacket (/.../node_modules/esbuild/lib/main.js:772:9)
    at Socket.readFromStdout (/.../node_modules/esbuild/lib/main.js:641:7)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) {
  errors: [
    {
      detail: undefined,
      id: '',
      location: {
        column: 26,
        file: '/.../file.ts',
        length: 1,
        line: 45,
        lineText: '\tinfer Item extends string,',
        namespace: '',
        suggestion: '?'
      },
      notes: [],
      pluginName: '',
      text: 'Expected "?" but found ","'
    }
  ],
  warnings: [],
  frame: '\n' +
    '\x1B[33mExpected "?" but found ","\x1B[39m\n' +
    '43 |  \n' +
    '44 |  type JoinDot = Items extends [\n' +
    '45 |  \tinfer Item extends string,\n' +
    '   |                            ^\n' +
    '46 |  \t...infer Rest extends string[],\n' +
    '47 |  ]\n',
  loc: {
    column: 26,
    file: '/.../file.ts',
    length: 1,
    line: 45,
    lineText: '\tinfer Item extends string,',
    namespace: '',
    suggestion: '?'
  },
  plugin: 'vite:esbuild',
  id: '/.../file.ts',
  pluginCode: '...'
}

I'm using vite but forced esbuild to the latest version 0.14.45. If this isn't an esbuild issue but rather a vite issue, apologies in advance!

@IgnusG
Copy link
Author

IgnusG commented Jun 18, 2022

I've added a test to ts_parser_test.go and it also seems to fail:

expectPrintedTS(t, "let x: A extends B<infer C extends D> ? D : never", "let x;\n")
--- FAIL: TestTSTypes (0.02s)
    --- FAIL: TestTSTypes/let_x:_A_extends_B<infer_C_extends_D>_?_D_:_never (0.00s)
        ts_parser_test.go:136: +<stdin>: ERROR: Expected "?" but found ">"

FAIL
FAIL    github.com/evanw/esbuild/internal/js_parser     0.674s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant