Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tsc/internal/checker/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ func (c *Checker) narrowTypeByDiscriminantProperty(t *Type, access *ast.Node, op
candidate := c.getConstituentTypeForKeyType(t, c.getTypeOfExpression(value))
if candidate != nil {
if assumeTrue && operator == ast.KindEqualsEqualsEqualsToken || !assumeTrue && operator == ast.KindExclamationEqualsEqualsToken {
return candidate
// Nullable constituents have no key property; preserve them as the general path does.
return c.getUnionType([]*Type{candidate, c.filterType(t, func(t *Type) bool { return t.flags&TypeFlagsNullable != 0 })})
}
if propType := c.getTypeOfPropertyOfType(candidate, keyPropertyName); propType != nil && isUnitType(propType) {
return c.removeType(t, candidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ if (working.thing!.name !== "Correct") {
>log : (...data: any[]) => void
>working.thing!.id : string
>working.thing! : { name: 'Correct'; id: string; }
>working.thing : { name: 'Correct'; id: string; }
>working.thing : { name: 'Correct'; id: string; } | undefined
>working : WorkingType
>thing : { name: 'Correct'; id: string; }
>thing : { name: 'Correct'; id: string; } | undefined
>id : string
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @strict: true
// @noEmit: true
// @noTypesAndSymbols: true

// Discriminant narrowing should be consistent regardless of union size (#62511).

type Small =
| { type: "1" }
| { type: "2" }
| undefined;

type Large =
| { type: "1" }
| { type: "2" }
| { type: "3" }
| { type: "4" }
| { type: "5" }
| { type: "6" }
| { type: "7" }
| { type: "8" }
| { type: "9" }
| { type: "10" }
| undefined;

declare let small: Small;
if (small!.type === "1") {
// @ts-expect-error
small.type;
}

declare let large: Large;
if (large!.type === "1") {
// @ts-expect-error
large.type;
}