Make literal types not comparable to weak object types without property overlap (#49865)

This commit is contained in:
Andrew Branch
2022-08-15 19:10:11 -05:00
committed by GitHub
parent 64ee1e88e0
commit 4e33e0eb6c
5 changed files with 219 additions and 1 deletions

View File

@@ -18861,7 +18861,8 @@ namespace ts {
}
}
const isPerformingCommonPropertyChecks = relation !== comparableRelation && !(intersectionState & IntersectionState.Target) &&
const isPerformingCommonPropertyChecks = (relation !== comparableRelation || relation === comparableRelation && isLiteralType(source)) &&
!(intersectionState & IntersectionState.Target) &&
source.flags & (TypeFlags.Primitive | TypeFlags.Object | TypeFlags.Intersection) && source !== globalObjectType &&
target.flags & (TypeFlags.Object | TypeFlags.Intersection) && isWeakType(target) &&
(getPropertiesOfType(source).length > 0 || typeHasCallOrConstructSignatures(source));

View File

@@ -0,0 +1,50 @@
//// [weakTypeAndPrimitiveNarrowing.ts]
type LiteralsAndWeakTypes =
| 'A'
| 'B'
| { optional?: true }
| { toLowerCase?(): string }
| { toUpperCase?(): string, otherOptionalProp?: number };
const g = (arg: LiteralsAndWeakTypes) => {
if (arg === 'A') {
arg;
} else {
arg;
}
}
type PrimitivesAndWeakTypes =
| string
| number
| { optional?: true }
| { toLowerCase?(): string }
| { toUpperCase?(): string, otherOptionalProp?: number };
const h = (arg: PrimitivesAndWeakTypes) => {
if (arg === 'A') {
arg;
} else {
arg;
}
}
//// [weakTypeAndPrimitiveNarrowing.js]
"use strict";
var g = function (arg) {
if (arg === 'A') {
arg;
}
else {
arg;
}
};
var h = function (arg) {
if (arg === 'A') {
arg;
}
else {
arg;
}
};

View File

@@ -0,0 +1,65 @@
=== tests/cases/compiler/weakTypeAndPrimitiveNarrowing.ts ===
type LiteralsAndWeakTypes =
>LiteralsAndWeakTypes : Symbol(LiteralsAndWeakTypes, Decl(weakTypeAndPrimitiveNarrowing.ts, 0, 0))
| 'A'
| 'B'
| { optional?: true }
>optional : Symbol(optional, Decl(weakTypeAndPrimitiveNarrowing.ts, 3, 5))
| { toLowerCase?(): string }
>toLowerCase : Symbol(toLowerCase, Decl(weakTypeAndPrimitiveNarrowing.ts, 4, 5))
| { toUpperCase?(): string, otherOptionalProp?: number };
>toUpperCase : Symbol(toUpperCase, Decl(weakTypeAndPrimitiveNarrowing.ts, 5, 5))
>otherOptionalProp : Symbol(otherOptionalProp, Decl(weakTypeAndPrimitiveNarrowing.ts, 5, 29))
const g = (arg: LiteralsAndWeakTypes) => {
>g : Symbol(g, Decl(weakTypeAndPrimitiveNarrowing.ts, 7, 5))
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 7, 11))
>LiteralsAndWeakTypes : Symbol(LiteralsAndWeakTypes, Decl(weakTypeAndPrimitiveNarrowing.ts, 0, 0))
if (arg === 'A') {
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 7, 11))
arg;
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 7, 11))
} else {
arg;
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 7, 11))
}
}
type PrimitivesAndWeakTypes =
>PrimitivesAndWeakTypes : Symbol(PrimitivesAndWeakTypes, Decl(weakTypeAndPrimitiveNarrowing.ts, 13, 1))
| string
| number
| { optional?: true }
>optional : Symbol(optional, Decl(weakTypeAndPrimitiveNarrowing.ts, 18, 5))
| { toLowerCase?(): string }
>toLowerCase : Symbol(toLowerCase, Decl(weakTypeAndPrimitiveNarrowing.ts, 19, 5))
| { toUpperCase?(): string, otherOptionalProp?: number };
>toUpperCase : Symbol(toUpperCase, Decl(weakTypeAndPrimitiveNarrowing.ts, 20, 5))
>otherOptionalProp : Symbol(otherOptionalProp, Decl(weakTypeAndPrimitiveNarrowing.ts, 20, 29))
const h = (arg: PrimitivesAndWeakTypes) => {
>h : Symbol(h, Decl(weakTypeAndPrimitiveNarrowing.ts, 22, 5))
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 22, 11))
>PrimitivesAndWeakTypes : Symbol(PrimitivesAndWeakTypes, Decl(weakTypeAndPrimitiveNarrowing.ts, 13, 1))
if (arg === 'A') {
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 22, 11))
arg;
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 22, 11))
} else {
arg;
>arg : Symbol(arg, Decl(weakTypeAndPrimitiveNarrowing.ts, 22, 11))
}
}

View File

@@ -0,0 +1,71 @@
=== tests/cases/compiler/weakTypeAndPrimitiveNarrowing.ts ===
type LiteralsAndWeakTypes =
>LiteralsAndWeakTypes : { optional?: true | undefined; } | { toLowerCase?(): string; } | { toUpperCase?(): string; otherOptionalProp?: number | undefined; } | "A" | "B"
| 'A'
| 'B'
| { optional?: true }
>optional : true | undefined
>true : true
| { toLowerCase?(): string }
>toLowerCase : (() => string) | undefined
| { toUpperCase?(): string, otherOptionalProp?: number };
>toUpperCase : (() => string) | undefined
>otherOptionalProp : number | undefined
const g = (arg: LiteralsAndWeakTypes) => {
>g : (arg: LiteralsAndWeakTypes) => void
>(arg: LiteralsAndWeakTypes) => { if (arg === 'A') { arg; } else { arg; }} : (arg: LiteralsAndWeakTypes) => void
>arg : LiteralsAndWeakTypes
if (arg === 'A') {
>arg === 'A' : boolean
>arg : LiteralsAndWeakTypes
>'A' : "A"
arg;
>arg : { toLowerCase?(): string; } | { toUpperCase?(): string; otherOptionalProp?: number | undefined; } | "A"
} else {
arg;
>arg : { optional?: true | undefined; } | { toLowerCase?(): string; } | { toUpperCase?(): string; otherOptionalProp?: number | undefined; } | "B"
}
}
type PrimitivesAndWeakTypes =
>PrimitivesAndWeakTypes : string | number | { optional?: true | undefined; } | { toLowerCase?(): string; } | { toUpperCase?(): string; otherOptionalProp?: number | undefined; }
| string
| number
| { optional?: true }
>optional : true | undefined
>true : true
| { toLowerCase?(): string }
>toLowerCase : (() => string) | undefined
| { toUpperCase?(): string, otherOptionalProp?: number };
>toUpperCase : (() => string) | undefined
>otherOptionalProp : number | undefined
const h = (arg: PrimitivesAndWeakTypes) => {
>h : (arg: PrimitivesAndWeakTypes) => void
>(arg: PrimitivesAndWeakTypes) => { if (arg === 'A') { arg; } else { arg; }} : (arg: PrimitivesAndWeakTypes) => void
>arg : PrimitivesAndWeakTypes
if (arg === 'A') {
>arg === 'A' : boolean
>arg : PrimitivesAndWeakTypes
>'A' : "A"
arg;
>arg : "A" | { toLowerCase?(): string; } | { toUpperCase?(): string; otherOptionalProp?: number | undefined; }
} else {
arg;
>arg : PrimitivesAndWeakTypes
}
}

View File

@@ -0,0 +1,31 @@
// @strict: true
type LiteralsAndWeakTypes =
| 'A'
| 'B'
| { optional?: true }
| { toLowerCase?(): string }
| { toUpperCase?(): string, otherOptionalProp?: number };
const g = (arg: LiteralsAndWeakTypes) => {
if (arg === 'A') {
arg;
} else {
arg;
}
}
type PrimitivesAndWeakTypes =
| string
| number
| { optional?: true }
| { toLowerCase?(): string }
| { toUpperCase?(): string, otherOptionalProp?: number };
const h = (arg: PrimitivesAndWeakTypes) => {
if (arg === 'A') {
arg;
} else {
arg;
}
}