TypeScript/tests/baselines/reference/narrowingTruthyObject.types
Anders Hejlsberg 6aeb8c12cc
Preserve type aliases for union and intersection types (#42149)
* Create separate types for equivalent aliased unions

* Accept new baselines

* Preserve original types for union types

* Accept new baselines

* Preserve intersection origin for union types

* Accept new baselines

* Accept new baselines

* Preserve aliases during relationship checks

* Accept new baselines

* Preserve aliases for intersection and indexed access types

* Accept new baselines

* Compute intersection-of-unions cross product without recursion

* Accept new baselines

* Use denormalized type objects for origin / support 'keyof' origins

* Accept new baselines

* Fix fourslash test

* Recursively extract named union types

* Accept new baselines

* Map on union origin in mapType to better preserve aliases and origins

* Remove redundant call

* Accept new baselines

* Revert back to declared type when branches produce equivalent union

* Accept new baselines

* Don't include denormal origin types in regular type statistics

* Fix issue with unions not being marked primitive-only

* Allow new alias to be associated with type alias instantiation

* Accept new baselines

* Revert "Accept new baselines"

This reverts commit 4507270cc1.

* Revert "Allow new alias to be associated with type alias instantiation"

This reverts commit 2c2d06dfe1.
2021-01-08 15:19:58 -10:00

142 lines
3.8 KiB
Plaintext

=== tests/cases/compiler/narrowingTruthyObject.ts ===
function foo(x: unknown, b: boolean) {
>foo : (x: unknown, b: boolean) => void
>x : unknown
>b : boolean
if (typeof x === 'object') {
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object | null
>toString : () => string
}
if (typeof x === 'object' && x) {
>typeof x === 'object' && x : false | object | null
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
>x : object | null
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (x && typeof x === 'object') {
>x && typeof x === 'object' : unknown
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (b && x && typeof x === 'object') {
>b && x && typeof x === 'object' : unknown
>b && x : unknown
>b : boolean
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (x && b && typeof x === 'object') {
>x && b && typeof x === 'object' : unknown
>x && b : unknown
>x : unknown
>b : boolean
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (x && b && b && typeof x === 'object') {
>x && b && b && typeof x === 'object' : unknown
>x && b && b : unknown
>x && b : unknown
>x : unknown
>b : boolean
>b : true
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
if (b && b && x && b && b && typeof x === 'object') {
>b && b && x && b && b && typeof x === 'object' : unknown
>b && b && x && b && b : unknown
>b && b && x && b : unknown
>b && b && x : unknown
>b && b : boolean
>b : boolean
>b : true
>x : unknown
>b : true
>b : true
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
x.toString();
>x.toString() : string
>x.toString : () => string
>x : object
>toString : () => string
}
}
// Repro from #36870
function f1(x: unknown): any {
>f1 : (x: unknown) => any
>x : unknown
return x && typeof x === 'object' && x.hasOwnProperty('x');
>x && typeof x === 'object' && x.hasOwnProperty('x') : unknown
>x && typeof x === 'object' : unknown
>x : unknown
>typeof x === 'object' : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>'object' : "object"
>x.hasOwnProperty('x') : boolean
>x.hasOwnProperty : (v: PropertyKey) => boolean
>x : object
>hasOwnProperty : (v: PropertyKey) => boolean
>'x' : "x"
}