Skip to content
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

Widen boolean literals when contextual type is full boolean type #48368

Closed
wants to merge 5 commits into from
Closed
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
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34206,8 +34206,8 @@ namespace ts {
function isLiteralOfContextualType(candidateType: Type, contextualType: Type | undefined): boolean {
if (contextualType) {
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
const types = (contextualType as UnionType).types;
return some(types, t => isLiteralOfContextualType(candidateType, t));
const skipBooleanLiterals = containsBooleanType(contextualType);
return some((contextualType as UnionType).types, t => !(skipBooleanLiterals && t.flags & TypeFlags.BooleanLiteral) && isLiteralOfContextualType(candidateType, t));
}
if (contextualType.flags & TypeFlags.InstantiableNonPrimitive) {
// If the contextual type is a type variable constrained to a primitive type, consider
Expand All @@ -34217,6 +34217,7 @@ namespace ts {
return maybeTypeOfKind(constraint, TypeFlags.String) && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) ||
maybeTypeOfKind(constraint, TypeFlags.Number) && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) ||
maybeTypeOfKind(constraint, TypeFlags.BigInt) && maybeTypeOfKind(candidateType, TypeFlags.BigIntLiteral) ||
containsBooleanType(constraint) && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) ||
maybeTypeOfKind(constraint, TypeFlags.ESSymbol) && maybeTypeOfKind(candidateType, TypeFlags.UniqueESSymbol) ||
isLiteralOfContextualType(candidateType, constraint);
}
Expand All @@ -34231,6 +34232,10 @@ namespace ts {
return false;
}

function containsBooleanType(type: Type) {
return !!(type.flags & TypeFlags.Union) && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType);
}

function isConstContext(node: Expression): boolean {
const parent = node.parent;
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(16,5): error TS2345: Argument of type '(string | number | boolean)[]' is not assignable to parameter of type '[string, number, boolean]'.
Target requires 3 element(s) but source may have fewer.
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(17,5): error TS2345: Argument of type '[string, number, true, ...(string | number | boolean)[]]' is not assignable to parameter of type '[string, number, boolean]'.
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(17,5): error TS2345: Argument of type '[string, number, boolean, ...(string | number | boolean)[]]' is not assignable to parameter of type '[string, number, boolean]'.
Target allows only 3 element(s) but source may have more.
tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextualTyping.ts(18,5): error TS2345: Argument of type '{ x: (string | number)[]; y: { c: boolean; d: string; e: number; }; }' is not assignable to parameter of type '{ x: [any, any]; y: { c: any; d: any; e: any; }; }'.
Types of property 'x' are incompatible.
Expand Down Expand Up @@ -30,7 +30,7 @@ tests/cases/conformance/expressions/contextualTyping/argumentExpressionContextua
!!! error TS2345: Target requires 3 element(s) but source may have fewer.
baz(["string", 1, true, ...array]); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[string, number, true, ...(string | number | boolean)[]]' is not assignable to parameter of type '[string, number, boolean]'.
!!! error TS2345: Argument of type '[string, number, boolean, ...(string | number | boolean)[]]' is not assignable to parameter of type '[string, number, boolean]'.
!!! error TS2345: Target allows only 3 element(s) but source may have more.
foo(o); // Error because x has an array type namely (string|number)[]
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ var o1: { x: [string, number], y: { c: boolean, d: string, e: number } } = { x:
>c : boolean
>d : string
>e : number
>{ x: ["string", 1], y: { c: true, d: "world", e: 3 } } : { x: [string, number]; y: { c: true; d: string; e: number; }; }
>{ x: ["string", 1], y: { c: true, d: "world", e: 3 } } : { x: [string, number]; y: { c: boolean; d: string; e: number; }; }
>x : [string, number]
>["string", 1] : [string, number]
>"string" : "string"
>1 : 1
>y : { c: true; d: string; e: number; }
>{ c: true, d: "world", e: 3 } : { c: true; d: string; e: number; }
>c : true
>y : { c: boolean; d: string; e: number; }
>{ c: true, d: "world", e: 3 } : { c: boolean; d: string; e: number; }
>c : boolean
>true : true
>d : string
>"world" : "world"
Expand Down Expand Up @@ -96,7 +96,7 @@ var array = ["string", 1, true];

var tuple: [string, number, boolean] = ["string", 1, true];
>tuple : [string, number, boolean]
>["string", 1, true] : [string, number, true]
>["string", 1, true] : [string, number, boolean]
>"string" : "string"
>1 : 1
>true : true
Expand All @@ -109,7 +109,7 @@ baz(tuple);
baz(["string", 1, true]);
>baz(["string", 1, true]) : void
>baz : (x: [string, number, boolean]) => void
>["string", 1, true] : [string, number, true]
>["string", 1, true] : [string, number, boolean]
>"string" : "string"
>1 : 1
>true : true
Expand All @@ -122,7 +122,7 @@ baz(array); // Error
baz(["string", 1, true, ...array]); // Error
>baz(["string", 1, true, ...array]) : void
>baz : (x: [string, number, boolean]) => void
>["string", 1, true, ...array] : [string, number, true, ...(string | number | boolean)[]]
>["string", 1, true, ...array] : [string, number, boolean, ...(string | number | boolean)[]]
>"string" : "string"
>1 : 1
>true : true
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/arrayBestCommonTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ module EmptyTypes {
>t2 : { x: boolean; y: base; }[]
>x : boolean
>y : base
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : ({ x: true; y: derived; } | { x: false; y: base; })[]
>{ x: true, y: new derived() } : { x: true; y: derived; }
>x : true
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : { x: boolean; y: derived; }[]
>{ x: true, y: new derived() } : { x: boolean; y: derived; }
>x : boolean
>true : true
>y : derived
>new derived() : derived
>derived : typeof derived
>{ x: false, y: new base() } : { x: false; y: base; }
>x : false
>{ x: false, y: new base() } : { x: boolean; y: base; }
>x : boolean
>false : false
>y : base
>new base() : base
Expand Down Expand Up @@ -645,15 +645,15 @@ module NonEmptyTypes {
>t2 : { x: boolean; y: base; }[]
>x : boolean
>y : base
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : ({ x: true; y: derived; } | { x: false; y: base; })[]
>{ x: true, y: new derived() } : { x: true; y: derived; }
>x : true
>[{ x: true, y: new derived() }, { x: false, y: new base() }] : { x: boolean; y: base; }[]
>{ x: true, y: new derived() } : { x: boolean; y: derived; }
>x : boolean
>true : true
>y : derived
>new derived() : derived
>derived : typeof derived
>{ x: false, y: new base() } : { x: false; y: base; }
>x : false
>{ x: false, y: new base() } : { x: boolean; y: base; }
>x : boolean
>false : false
>y : base
>new base() : base
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/arrayLiteralInference.types
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,21 @@ declare function foo<T>(...args: T[]): T[];
let b1: { x: boolean }[] = foo({ x: true }, { x: false });
>b1 : { x: boolean; }[]
>x : boolean
>foo({ x: true }, { x: false }) : ({ x: true; } | { x: false; })[]
>foo({ x: true }, { x: false }) : { x: boolean; }[]
>foo : <T>(...args: T[]) => T[]
>{ x: true } : { x: true; }
>x : true
>{ x: true } : { x: boolean; }
>x : boolean
>true : true
>{ x: false } : { x: false; }
>x : false
>{ x: false } : { x: boolean; }
>x : boolean
>false : false

let b2: boolean[][] = foo([true], [false]);
>b2 : boolean[][]
>foo([true], [false]) : (true[] | false[])[]
>foo([true], [false]) : boolean[][]
>foo : <T>(...args: T[]) => T[]
>[true] : true[]
>[true] : boolean[]
>true : true
>[false] : false[]
>[false] : boolean[]
>false : false

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/arrayLiteralTypeInference.ts(14,14): error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(14,14): error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(15,14): error TS2322: Type '{ id: number; name: string; }' is not assignable to type 'Action'.
Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(31,18): error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type '{ id: number; }'.
tests/cases/compiler/arrayLiteralTypeInference.ts(31,18): error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
Expand All @@ -24,7 +24,7 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{
var x1: Action[] = [
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type 'Action'.
!!! error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
{ id: 3, name: "three" }
~~~~~~~~~~~~~
Expand All @@ -47,7 +47,7 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{
[
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
{ id: 3, name: "three" }
~~~~~~~~~~~~~
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/assignToFn.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module M {

var x:I={ f:function(n) { return true; } };
>x : I
>{ f:function(n) { return true; } } : { f: (n: number) => true; }
>f : (n: number) => true
>function(n) { return true; } : (n: number) => true
>{ f:function(n) { return true; } } : { f: (n: number) => boolean; }
>f : (n: number) => boolean
>function(n) { return true; } : (n: number) => boolean
>n : number
>true : true

Expand Down
26 changes: 13 additions & 13 deletions tests/baselines/reference/assignmentTypeNarrowing.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ x; // string
>x : string

[x] = [true];
>[x] = [true] : [true]
>[x] = [true] : [boolean]
>[x] : [string | number | boolean | RegExp]
>x : string | number | boolean | RegExp
>[true] : [true]
>[true] : [boolean]
>true : true

x; // boolean
>x : true
>x : boolean

[x = ""] = [1];
>[x = ""] = [1] : [number]
Expand All @@ -33,16 +33,16 @@ x; // string | number
>x : string | number

({x} = {x: true});
>({x} = {x: true}) : { x: true; }
>{x} = {x: true} : { x: true; }
>({x} = {x: true}) : { x: boolean; }
>{x} = {x: true} : { x: boolean; }
>{x} : { x: string | number | boolean | RegExp; }
>x : string | number | boolean | RegExp
>{x: true} : { x: true; }
>x : true
>{x: true} : { x: boolean; }
>x : boolean
>true : true

x; // boolean
>x : true
>x : boolean

({y: x} = {y: 1});
>({y: x} = {y: 1}) : { y: number; }
Expand All @@ -58,17 +58,17 @@ x; // number
>x : number

({x = ""} = {x: true});
>({x = ""} = {x: true}) : { x?: true; }
>{x = ""} = {x: true} : { x?: true; }
>({x = ""} = {x: true}) : { x?: boolean; }
>{x = ""} = {x: true} : { x?: boolean; }
>{x = ""} : { x?: string | number | boolean | RegExp; }
>x : string | number | boolean | RegExp
>"" : ""
>{x: true} : { x?: true; }
>x : true
>{x: true} : { x?: boolean; }
>x : boolean
>true : true

x; // string | boolean
>x : string | true
>x : string | boolean

({y: x = /a/} = {y: 1});
>({y: x = /a/} = {y: 1}) : { y?: number; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function fAsyncExplicit(): Promise<[number, boolean]> {

// This is contextually typed as a tuple.
return [1, true];
>[1, true] : [number, true]
>[1, true] : [number, boolean]
>1 : 1
>true : true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/awaitedType.types
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async function main() {
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>Promise : PromiseConstructor
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>[ MaybePromise(1), MaybePromise('2'), MaybePromise(true), ] : [number | Promise<1> | PromiseLike<1>, string | Promise<"2"> | PromiseLike<"2">, MaybePromise<true>]
>[ MaybePromise(1), MaybePromise('2'), MaybePromise(true), ] : [number | Promise<1> | PromiseLike<1>, string | Promise<"2"> | PromiseLike<"2">, boolean | Promise<true> | PromiseLike<true>]

MaybePromise(1),
>MaybePromise(1) : 1 | Promise<1> | PromiseLike<1>
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/awaitedTypeStrictNull.types
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async function main() {
>Promise.all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>Promise : PromiseConstructor
>all : { <T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>; <T extends [] | readonly unknown[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]>; }>; }
>[ MaybePromise(1), MaybePromise('2'), MaybePromise(true), ] : [number | Promise<1> | PromiseLike<1>, string | Promise<"2"> | PromiseLike<"2">, MaybePromise<true>]
>[ MaybePromise(1), MaybePromise('2'), MaybePromise(true), ] : [number | Promise<1> | PromiseLike<1>, string | Promise<"2"> | PromiseLike<"2">, boolean | Promise<true> | PromiseLike<true>]

MaybePromise(1),
>MaybePromise(1) : 1 | Promise<1> | PromiseLike<1>
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callsOnComplexSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ function test5() {
>a : JSX.Element
><C p={true} /> : JSX.Element
>C : React.ComponentType<P1> | React.ComponentType<P2>
>p : true
>p : boolean
>true : true
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/checkExportsObjectAssignProperty.types
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ Object.defineProperty(exports, "thing", { value: 42, writable: true });
>defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T
>exports : typeof import("tests/cases/conformance/jsdoc/mod1")
>"thing" : "thing"
>{ value: 42, writable: true } : { value: number; writable: true; }
>{ value: 42, writable: true } : { value: number; writable: boolean; }
>value : number
>42 : 42
>writable : true
>writable : boolean
>true : true

Object.defineProperty(exports, "readonlyProp", { value: "Smith", writable: false });
Expand All @@ -194,10 +194,10 @@ Object.defineProperty(exports, "readonlyProp", { value: "Smith", writable: false
>defineProperty : <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T
>exports : typeof import("tests/cases/conformance/jsdoc/mod1")
>"readonlyProp" : "readonlyProp"
>{ value: "Smith", writable: false } : { value: string; writable: false; }
>{ value: "Smith", writable: false } : { value: string; writable: boolean; }
>value : string
>"Smith" : "Smith"
>writable : false
>writable : boolean
>false : false

Object.defineProperty(exports, "rwAccessors", { get() { return 98122 }, set(_) { /*ignore*/ } });
Expand Down Expand Up @@ -259,10 +259,10 @@ Object.defineProperty(module.exports, "thing", { value: "yes", writable: true })
>module : { exports: typeof module.exports; }
>exports : typeof module.exports
>"thing" : "thing"
>{ value: "yes", writable: true } : { value: string; writable: true; }
>{ value: "yes", writable: true } : { value: string; writable: boolean; }
>value : string
>"yes" : "yes"
>writable : true
>writable : boolean
>true : true

Object.defineProperty(module.exports, "readonlyProp", { value: "Smith", writable: false });
Expand All @@ -274,10 +274,10 @@ Object.defineProperty(module.exports, "readonlyProp", { value: "Smith", writable
>module : { exports: typeof module.exports; }
>exports : typeof module.exports
>"readonlyProp" : "readonlyProp"
>{ value: "Smith", writable: false } : { value: string; writable: false; }
>{ value: "Smith", writable: false } : { value: string; writable: boolean; }
>value : string
>"Smith" : "Smith"
>writable : false
>writable : boolean
>false : false

Object.defineProperty(module.exports, "rwAccessors", { get() { return 98122 }, set(_) { /*ignore*/ } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ Object.defineProperty(Person.prototype, "thing", { value: 42, writable: true });
>Person : typeof Person
>prototype : any
>"thing" : "thing"
>{ value: 42, writable: true } : { value: number; writable: true; }
>{ value: 42, writable: true } : { value: number; writable: boolean; }
>value : number
>42 : 42
>writable : true
>writable : boolean
>true : true

Object.defineProperty(Person.prototype, "readonlyProp", { value: "Smith", writable: false });
Expand All @@ -151,10 +151,10 @@ Object.defineProperty(Person.prototype, "readonlyProp", { value: "Smith", writab
>Person : typeof Person
>prototype : any
>"readonlyProp" : "readonlyProp"
>{ value: "Smith", writable: false } : { value: string; writable: false; }
>{ value: "Smith", writable: false } : { value: string; writable: boolean; }
>value : string
>"Smith" : "Smith"
>writable : false
>writable : boolean
>false : false

Object.defineProperty(Person.prototype, "rwAccessors", { get() { return 98122 }, set(_) { /*ignore*/ } });
Expand Down
Loading