From dacf97e7c8149902574a5e983a5baf0639876942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 29 Dec 2024 23:36:50 +0100 Subject: [PATCH] Ignore `silentNever` in intersections with other meaningful constituents --- src/compiler/checker.ts | 3 + .../silentNeverInIntersections1.symbols | 99 +++++++++++++++++++ .../silentNeverInIntersections1.types | 89 +++++++++++++++++ .../compiler/silentNeverInIntersections1.ts | 33 +++++++ 4 files changed, 224 insertions(+) create mode 100644 tests/baselines/reference/silentNeverInIntersections1.symbols create mode 100644 tests/baselines/reference/silentNeverInIntersections1.types create mode 100644 tests/cases/compiler/silentNeverInIntersections1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 80b61edd3657a..3c34bc15fe977 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -17825,6 +17825,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function addTypeToIntersection(typeSet: Map, includes: TypeFlags, type: Type) { + if (type === silentNeverType) { + return includes; + } const flags = type.flags; if (flags & TypeFlags.Intersection) { return addTypesToIntersection(typeSet, includes, (type as IntersectionType).types); diff --git a/tests/baselines/reference/silentNeverInIntersections1.symbols b/tests/baselines/reference/silentNeverInIntersections1.symbols new file mode 100644 index 0000000000000..fd53dd580e79c --- /dev/null +++ b/tests/baselines/reference/silentNeverInIntersections1.symbols @@ -0,0 +1,99 @@ +//// [tests/cases/compiler/silentNeverInIntersections1.ts] //// + +=== silentNeverInIntersections1.ts === +// https://github.com/microsoft/TypeScript/issues/60864 + +class Variable { +>Variable : Symbol(Variable, Decl(silentNeverInIntersections1.ts, 0, 0)) +>U : Symbol(U, Decl(silentNeverInIntersections1.ts, 2, 15)) +>S : Symbol(S, Decl(silentNeverInIntersections1.ts, 2, 17)) + + constructor(public s: S) {} +>s : Symbol(Variable.s, Decl(silentNeverInIntersections1.ts, 3, 14)) +>S : Symbol(S, Decl(silentNeverInIntersections1.ts, 2, 17)) + + u!: U; +>u : Symbol(Variable.u, Decl(silentNeverInIntersections1.ts, 3, 29)) +>U : Symbol(U, Decl(silentNeverInIntersections1.ts, 2, 15)) +} + +function mkGeneric(s: S) { +>mkGeneric : Symbol(mkGeneric, Decl(silentNeverInIntersections1.ts, 5, 1)) +>U : Symbol(U, Decl(silentNeverInIntersections1.ts, 7, 19)) +>S : Symbol(S, Decl(silentNeverInIntersections1.ts, 7, 21)) +>s : Symbol(s, Decl(silentNeverInIntersections1.ts, 7, 40)) +>S : Symbol(S, Decl(silentNeverInIntersections1.ts, 7, 21)) + + return new Variable(s); +>Variable : Symbol(Variable, Decl(silentNeverInIntersections1.ts, 0, 0)) +>U : Symbol(U, Decl(silentNeverInIntersections1.ts, 7, 19)) +>S : Symbol(S, Decl(silentNeverInIntersections1.ts, 7, 21)) +>s : Symbol(s, Decl(silentNeverInIntersections1.ts, 7, 40)) +} + +type ExactArgNames = GenericType & { +>ExactArgNames : Symbol(ExactArgNames, Decl(silentNeverInIntersections1.ts, 9, 1)) +>GenericType : Symbol(GenericType, Decl(silentNeverInIntersections1.ts, 11, 19)) +>Constraint : Symbol(Constraint, Decl(silentNeverInIntersections1.ts, 11, 31)) +>GenericType : Symbol(GenericType, Decl(silentNeverInIntersections1.ts, 11, 19)) + + [K in keyof GenericType]: K extends keyof Constraint ? GenericType[K] : never; +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 12, 3)) +>GenericType : Symbol(GenericType, Decl(silentNeverInIntersections1.ts, 11, 19)) +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 12, 3)) +>Constraint : Symbol(Constraint, Decl(silentNeverInIntersections1.ts, 11, 31)) +>GenericType : Symbol(GenericType, Decl(silentNeverInIntersections1.ts, 11, 19)) +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 12, 3)) + +}; + +type AllowVariables = +>AllowVariables : Symbol(AllowVariables, Decl(silentNeverInIntersections1.ts, 13, 2)) +>T : Symbol(T, Decl(silentNeverInIntersections1.ts, 15, 20)) + + | Variable +>Variable : Symbol(Variable, Decl(silentNeverInIntersections1.ts, 0, 0)) +>T : Symbol(T, Decl(silentNeverInIntersections1.ts, 15, 20)) + + | { [K in keyof T]: Variable | T[K] }; +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 17, 7)) +>T : Symbol(T, Decl(silentNeverInIntersections1.ts, 15, 20)) +>Variable : Symbol(Variable, Decl(silentNeverInIntersections1.ts, 0, 0)) +>T : Symbol(T, Decl(silentNeverInIntersections1.ts, 15, 20)) +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 17, 7)) +>T : Symbol(T, Decl(silentNeverInIntersections1.ts, 15, 20)) +>K : Symbol(K, Decl(silentNeverInIntersections1.ts, 17, 7)) + +type TestArgs = { +>TestArgs : Symbol(TestArgs, Decl(silentNeverInIntersections1.ts, 17, 51)) + + someArg: number; +>someArg : Symbol(someArg, Decl(silentNeverInIntersections1.ts, 19, 17)) + +}; + +type TestArgsWithVars = AllowVariables; +>TestArgsWithVars : Symbol(TestArgsWithVars, Decl(silentNeverInIntersections1.ts, 21, 2)) +>AllowVariables : Symbol(AllowVariables, Decl(silentNeverInIntersections1.ts, 13, 2)) +>TestArgs : Symbol(TestArgs, Decl(silentNeverInIntersections1.ts, 17, 51)) + +function takesGeneric>( +>takesGeneric : Symbol(takesGeneric, Decl(silentNeverInIntersections1.ts, 23, 49)) +>V : Symbol(V, Decl(silentNeverInIntersections1.ts, 25, 22)) +>AllowVariables : Symbol(AllowVariables, Decl(silentNeverInIntersections1.ts, 13, 2)) +>TestArgs : Symbol(TestArgs, Decl(silentNeverInIntersections1.ts, 17, 51)) + + a: ExactArgNames, +>a : Symbol(a, Decl(silentNeverInIntersections1.ts, 25, 58)) +>ExactArgNames : Symbol(ExactArgNames, Decl(silentNeverInIntersections1.ts, 9, 1)) +>V : Symbol(V, Decl(silentNeverInIntersections1.ts, 25, 22)) +>TestArgs : Symbol(TestArgs, Decl(silentNeverInIntersections1.ts, 17, 51)) + +): void {} + +let v = takesGeneric({ someArg: mkGeneric("x") }); +>v : Symbol(v, Decl(silentNeverInIntersections1.ts, 29, 3)) +>takesGeneric : Symbol(takesGeneric, Decl(silentNeverInIntersections1.ts, 23, 49)) +>someArg : Symbol(someArg, Decl(silentNeverInIntersections1.ts, 29, 22)) +>mkGeneric : Symbol(mkGeneric, Decl(silentNeverInIntersections1.ts, 5, 1)) + diff --git a/tests/baselines/reference/silentNeverInIntersections1.types b/tests/baselines/reference/silentNeverInIntersections1.types new file mode 100644 index 0000000000000..0ed801ed2edd0 --- /dev/null +++ b/tests/baselines/reference/silentNeverInIntersections1.types @@ -0,0 +1,89 @@ +//// [tests/cases/compiler/silentNeverInIntersections1.ts] //// + +=== silentNeverInIntersections1.ts === +// https://github.com/microsoft/TypeScript/issues/60864 + +class Variable { +>Variable : Variable +> : ^^^^^^^^^^^^^^ + + constructor(public s: S) {} +>s : S +> : ^ + + u!: U; +>u : U +> : ^ +} + +function mkGeneric(s: S) { +>mkGeneric : (s: S) => Variable +> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^ +>s : S +> : ^ + + return new Variable(s); +>new Variable(s) : Variable +> : ^^^^^^^^^^^^^^ +>Variable : typeof Variable +> : ^^^^^^^^^^^^^^^ +>s : S +> : ^ +} + +type ExactArgNames = GenericType & { +>ExactArgNames : ExactArgNames +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + [K in keyof GenericType]: K extends keyof Constraint ? GenericType[K] : never; +}; + +type AllowVariables = +>AllowVariables : AllowVariables +> : ^^^^^^^^^^^^^^^^^ + + | Variable + | { [K in keyof T]: Variable | T[K] }; + +type TestArgs = { +>TestArgs : TestArgs +> : ^^^^^^^^ + + someArg: number; +>someArg : number +> : ^^^^^^ + +}; + +type TestArgsWithVars = AllowVariables; +>TestArgsWithVars : TestArgsWithVars +> : ^^^^^^^^^^^^^^^^ + +function takesGeneric>( +>takesGeneric : >(a: ExactArgNames) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ + + a: ExactArgNames, +>a : ExactArgNames +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +): void {} + +let v = takesGeneric({ someArg: mkGeneric("x") }); +>v : void +> : ^^^^ +>takesGeneric({ someArg: mkGeneric("x") }) : void +> : ^^^^ +>takesGeneric : >(a: ExactArgNames) => void +> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ +>{ someArg: mkGeneric("x") } : { someArg: Variable; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>someArg : Variable +> : ^^^^^^^^^^^^^^^^^^^^^ +>mkGeneric("x") : Variable +> : ^^^^^^^^^^^^^^^^^^^^^ +>mkGeneric : (s: S) => Variable +> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^ +>"x" : "x" +> : ^^^ + diff --git a/tests/cases/compiler/silentNeverInIntersections1.ts b/tests/cases/compiler/silentNeverInIntersections1.ts new file mode 100644 index 0000000000000..c19dfc07ae579 --- /dev/null +++ b/tests/cases/compiler/silentNeverInIntersections1.ts @@ -0,0 +1,33 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/60864 + +class Variable { + constructor(public s: S) {} + u!: U; +} + +function mkGeneric(s: S) { + return new Variable(s); +} + +type ExactArgNames = GenericType & { + [K in keyof GenericType]: K extends keyof Constraint ? GenericType[K] : never; +}; + +type AllowVariables = + | Variable + | { [K in keyof T]: Variable | T[K] }; + +type TestArgs = { + someArg: number; +}; + +type TestArgsWithVars = AllowVariables; + +function takesGeneric>( + a: ExactArgNames, +): void {} + +let v = takesGeneric({ someArg: mkGeneric("x") });