Skip to content

Port "Improve type discrimination algorithm" from tsgo #61828

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

Merged
merged 4 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24802,11 +24802,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
for (let i = 0; i < types.length; i++) {
if (include[i]) {
const targetType = getTypeOfPropertyOrIndexSignatureOfType(types[i], propertyName);
if (targetType && someType(getDiscriminatingType(), t => !!related(t, targetType))) {
matched = true;
}
else {
include[i] = Ternary.Maybe;
if (targetType) {
if (someType(getDiscriminatingType(), t => !!related(t, targetType))) {
matched = true;
}
else {
include[i] = Ternary.Maybe;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function createImportAdderWorker(sourceFile: SourceFile | FutureSourceFile, prog
);

let fix: FixAddNewImport | ImportFixWithModuleSpecifier;
if (existingFix && importKind !== ImportKind.Namespace) {
if (existingFix && importKind !== ImportKind.Namespace && existingFix.kind !== ImportFixKind.UseNamespace && existingFix.kind !== ImportFixKind.JsdocTypeImport) {
Copy link
Preview

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This boolean condition is getting lengthy and complex; consider extracting the kind checks into a well-named helper function (e.g., shouldReuseImportFix(existingFix, importKind)) for readability.

Copilot uses AI. Check for mistakes.

fix = {
...existingFix,
addAsTypeOnly,
Comment on lines 374 to 376
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke because fix is a union of objects, except the two props added here are not actually in two of the fixes, so the code was actually tacking random props onto fixes that would then simply never be read by anyone.

Expand Down

This file was deleted.

28 changes: 28 additions & 0 deletions tests/baselines/reference/missingDiscriminants.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
missingDiscriminants.ts(17,23): error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'.
missingDiscriminants.ts(18,34): error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'.


==== missingDiscriminants.ts (2 errors) ====
// https://github.com/microsoft/typescript-go/issues/1020

type Thing =
| { str: "a", num: 0 }
| { str: "b" }
| { num: 1 }

const thing1: Thing = { str: "a", num: 0 }
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error

type Item =
| { kind: "a", subkind: 0, value: string }
| { kind: "a", subkind: 1, value: number }
| { kind: "b" }

const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a"
~~~~~~~
!!! error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'.
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property
~~~~~~~
!!! error TS2353: Object literal may only specify known properties, and 'subkind' does not exist in type '{ kind: "b"; }'.

64 changes: 64 additions & 0 deletions tests/baselines/reference/missingDiscriminants.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/missingDiscriminants.ts] ////

=== missingDiscriminants.ts ===
// https://github.com/microsoft/typescript-go/issues/1020

type Thing =
>Thing : Symbol(Thing, Decl(missingDiscriminants.ts, 0, 0))

| { str: "a", num: 0 }
>str : Symbol(str, Decl(missingDiscriminants.ts, 3, 5))
>num : Symbol(num, Decl(missingDiscriminants.ts, 3, 15))

| { str: "b" }
>str : Symbol(str, Decl(missingDiscriminants.ts, 4, 5))

| { num: 1 }
>num : Symbol(num, Decl(missingDiscriminants.ts, 5, 5))

const thing1: Thing = { str: "a", num: 0 }
>thing1 : Symbol(thing1, Decl(missingDiscriminants.ts, 7, 5))
>Thing : Symbol(Thing, Decl(missingDiscriminants.ts, 0, 0))
>str : Symbol(str, Decl(missingDiscriminants.ts, 7, 23))
>num : Symbol(num, Decl(missingDiscriminants.ts, 7, 33))

const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error
>thing2 : Symbol(thing2, Decl(missingDiscriminants.ts, 8, 5))
>Thing : Symbol(Thing, Decl(missingDiscriminants.ts, 0, 0))
>str : Symbol(str, Decl(missingDiscriminants.ts, 8, 23))
>num : Symbol(num, Decl(missingDiscriminants.ts, 8, 33))

const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error
>thing3 : Symbol(thing3, Decl(missingDiscriminants.ts, 9, 5))
>Thing : Symbol(Thing, Decl(missingDiscriminants.ts, 0, 0))
>num : Symbol(num, Decl(missingDiscriminants.ts, 9, 23))
>str : Symbol(str, Decl(missingDiscriminants.ts, 9, 31))

type Item =
>Item : Symbol(Item, Decl(missingDiscriminants.ts, 9, 42))

| { kind: "a", subkind: 0, value: string }
>kind : Symbol(kind, Decl(missingDiscriminants.ts, 12, 5))
>subkind : Symbol(subkind, Decl(missingDiscriminants.ts, 12, 16))
>value : Symbol(value, Decl(missingDiscriminants.ts, 12, 28))

| { kind: "a", subkind: 1, value: number }
>kind : Symbol(kind, Decl(missingDiscriminants.ts, 13, 5))
>subkind : Symbol(subkind, Decl(missingDiscriminants.ts, 13, 16))
>value : Symbol(value, Decl(missingDiscriminants.ts, 13, 28))

| { kind: "b" }
>kind : Symbol(kind, Decl(missingDiscriminants.ts, 14, 5))

const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a"
>item1 : Symbol(item1, Decl(missingDiscriminants.ts, 16, 5))
>Item : Symbol(Item, Decl(missingDiscriminants.ts, 9, 42))
>subkind : Symbol(subkind, Decl(missingDiscriminants.ts, 16, 21))
>kind : Symbol(kind, Decl(missingDiscriminants.ts, 16, 33))

const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property
>item2 : Symbol(item2, Decl(missingDiscriminants.ts, 17, 5))
>Item : Symbol(Item, Decl(missingDiscriminants.ts, 9, 42))
>kind : Symbol(kind, Decl(missingDiscriminants.ts, 17, 21))
>subkind : Symbol(subkind, Decl(missingDiscriminants.ts, 17, 32))

117 changes: 117 additions & 0 deletions tests/baselines/reference/missingDiscriminants.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//// [tests/cases/compiler/missingDiscriminants.ts] ////

=== missingDiscriminants.ts ===
// https://github.com/microsoft/typescript-go/issues/1020

type Thing =
>Thing : Thing
> : ^^^^^

| { str: "a", num: 0 }
>str : "a"
> : ^^^
>num : 0
> : ^

| { str: "b" }
>str : "b"
> : ^^^

| { num: 1 }
>num : 1
> : ^

const thing1: Thing = { str: "a", num: 0 }
>thing1 : Thing
> : ^^^^^
>{ str: "a", num: 0 } : { str: "a"; num: 0; }
> : ^^^^^^^^^^^^^^^^^^^^^
>str : "a"
> : ^^^
>"a" : "a"
> : ^^^
>num : 0
> : ^
>0 : 0
> : ^

const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error
>thing2 : Thing
> : ^^^^^
>{ str: "b", num: 1 } : { str: "b"; num: 1; }
> : ^^^^^^^^^^^^^^^^^^^^^
>str : "b"
> : ^^^
>"b" : "b"
> : ^^^
>num : 1
> : ^
>1 : 1
> : ^

const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error
>thing3 : Thing
> : ^^^^^
>{ num: 1, str: "b" } : { num: 1; str: "b"; }
> : ^^^^^^^^^^^^^^^^^^^^^
>num : 1
> : ^
>1 : 1
> : ^
>str : "b"
> : ^^^
>"b" : "b"
> : ^^^

type Item =
>Item : Item
> : ^^^^

| { kind: "a", subkind: 0, value: string }
>kind : "a"
> : ^^^
>subkind : 0
> : ^
>value : string
> : ^^^^^^

| { kind: "a", subkind: 1, value: number }
>kind : "a"
> : ^^^
>subkind : 1
> : ^
>value : number
> : ^^^^^^

| { kind: "b" }
>kind : "b"
> : ^^^

const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a"
>item1 : Item
> : ^^^^
>{ subkind: 1, kind: "b" } : { subkind: number; kind: "b"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>subkind : number
> : ^^^^^^
>1 : 1
> : ^
>kind : "b"
> : ^^^
>"b" : "b"
> : ^^^

const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property
>item2 : Item
> : ^^^^
>{ kind: "b", subkind: 1 } : { kind: "b"; subkind: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>kind : "b"
> : ^^^
>"b" : "b"
> : ^^^
>subkind : number
> : ^^^^^^
>1 : 1
> : ^

2 changes: 1 addition & 1 deletion tests/baselines/reference/promiseType.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/promiseType.ts] ////

=== Performance Stats ===
Instantiation count: 2,500
Instantiation count: 1,000

=== promiseType.ts ===
declare var p: Promise<boolean>;
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/compiler/missingDiscriminants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/typescript-go/issues/1020

type Thing =
| { str: "a", num: 0 }
| { str: "b" }
| { num: 1 }

const thing1: Thing = { str: "a", num: 0 }
const thing2: Thing = { str: "b", num: 1 } // Shouldn't be error
const thing3: Thing = { num: 1, str: "b" } // Shouldn't be error

type Item =
| { kind: "a", subkind: 0, value: string }
| { kind: "a", subkind: 1, value: number }
| { kind: "b" }

const item1: Item = { subkind: 1, kind: "b" } // Error, type "b" not assignable to type "a"
const item2: Item = { kind: "b", subkind: 1 } // Error, 'subkind' isn't a known property