diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d450d64bcb..3ea51850c13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +* Fix a bug with TypeScript type parsing ([#3574](https://github.com/evanw/esbuild/issues/3574)) + + This release fixes a bug with esbuild's TypeScript parser where a conditional type containing a union type that ends with an infer type that ends with a constraint could fail to parse. This was caused by the "don't parse a conditional type" flag not getting passed through the union type parser. Here's an example of valid TypeScript code that previously failed to parse correctly: + + ```ts + type InferUnion = T extends { a: infer U extends number } | infer U extends number ? U : never + ``` + ## 0.19.11 * Fix TypeScript-specific class transform edge case ([#3559](https://github.com/evanw/esbuild/issues/3559)) diff --git a/internal/js_parser/ts_parser.go b/internal/js_parser/ts_parser.go index aa721e8aafc..918437bd601 100644 --- a/internal/js_parser/ts_parser.go +++ b/internal/js_parser/ts_parser.go @@ -482,14 +482,14 @@ loop: return } p.lexer.Next() - p.skipTypeScriptType(js_ast.LBitwiseOr) + p.skipTypeScriptTypeWithFlags(js_ast.LBitwiseOr, flags) case js_lexer.TAmpersand: if level >= js_ast.LBitwiseAnd { return } p.lexer.Next() - p.skipTypeScriptType(js_ast.LBitwiseAnd) + p.skipTypeScriptTypeWithFlags(js_ast.LBitwiseAnd, flags) case js_lexer.TExclamation: // A postfix "!" is allowed in JSDoc types in TypeScript, which are only diff --git a/internal/js_parser/ts_parser_test.go b/internal/js_parser/ts_parser_test.go index 892bbbc48b3..7660e23dcb3 100644 --- a/internal/js_parser/ts_parser_test.go +++ b/internal/js_parser/ts_parser_test.go @@ -279,6 +279,10 @@ func TestTSTypes(t *testing.T) { expectPrintedTS(t, "type Foo = Bar extends [infer T extends string] ? T : null", "") expectPrintedTS(t, "type Foo = {} extends infer T extends {} ? A : never", "") expectPrintedTS(t, "type Foo = {} extends (infer T extends {}) ? A : never", "") + expectPrintedTS(t, "type Foo = T extends { a: infer U extends number } | { b: infer U extends number } ? U : never", "") + expectPrintedTS(t, "type Foo = T extends { a: infer U extends number } & { b: infer U extends number } ? U : never", "") + expectPrintedTS(t, "type Foo = T extends { a: infer U extends number } | infer U extends number ? U : never", "") + expectPrintedTS(t, "type Foo = T extends { a: infer U extends number } & infer U extends number ? U : never", "") expectPrintedTS(t, "let x: A extends B ? D : never", "let x;\n") expectPrintedTS(t, "let x: A extends B ? D : never", "let x;\n") expectPrintedTS(t, "let x: ([e1, e2, ...es]: any) => any", "let x;\n")