Skip to content

Commit 35c0d65

Browse files
committed
fix #3574: ts type parser bug with infer + extends
1 parent f6eae0c commit 35c0d65

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix a bug with TypeScript type parsing ([#3574](https://github.com/evanw/esbuild/issues/3574))
6+
7+
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:
8+
9+
```ts
10+
type InferUnion<T> = T extends { a: infer U extends number } | infer U extends number ? U : never
11+
```
12+
313
## 0.19.11
414
515
* Fix TypeScript-specific class transform edge case ([#3559](https://github.com/evanw/esbuild/issues/3559))

internal/js_parser/ts_parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ loop:
482482
return
483483
}
484484
p.lexer.Next()
485-
p.skipTypeScriptType(js_ast.LBitwiseOr)
485+
p.skipTypeScriptTypeWithFlags(js_ast.LBitwiseOr, flags)
486486

487487
case js_lexer.TAmpersand:
488488
if level >= js_ast.LBitwiseAnd {
489489
return
490490
}
491491
p.lexer.Next()
492-
p.skipTypeScriptType(js_ast.LBitwiseAnd)
492+
p.skipTypeScriptTypeWithFlags(js_ast.LBitwiseAnd, flags)
493493

494494
case js_lexer.TExclamation:
495495
// A postfix "!" is allowed in JSDoc types in TypeScript, which are only

internal/js_parser/ts_parser_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ func TestTSTypes(t *testing.T) {
279279
expectPrintedTS(t, "type Foo = Bar extends [infer T extends string] ? T : null", "")
280280
expectPrintedTS(t, "type Foo = {} extends infer T extends {} ? A<T> : never", "")
281281
expectPrintedTS(t, "type Foo = {} extends (infer T extends {}) ? A<T> : never", "")
282+
expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } | { b: infer U extends number } ? U : never", "")
283+
expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } & { b: infer U extends number } ? U : never", "")
284+
expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } | infer U extends number ? U : never", "")
285+
expectPrintedTS(t, "type Foo<T> = T extends { a: infer U extends number } & infer U extends number ? U : never", "")
282286
expectPrintedTS(t, "let x: A extends B<infer C extends D> ? D : never", "let x;\n")
283287
expectPrintedTS(t, "let x: A extends B<infer C extends D ? infer C : never> ? D : never", "let x;\n")
284288
expectPrintedTS(t, "let x: ([e1, e2, ...es]: any) => any", "let x;\n")

0 commit comments

Comments
 (0)