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

more precise type facts for intersection #47282

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
261 changes: 258 additions & 3 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions tests/baselines/reference/narrowingTypeof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [narrowingTypeof.ts]
type __String = (string & { __escapedIdentifier: void }) | (void & { __escapedIdentifier: void });

declare const s: __String;
declare let t: string | number | undefined;

declare function assert(e: unknown): asserts e;

assert(typeof s === "string");
t = s;


//// [narrowingTypeof.js]
assert(typeof s === "string");
t = s;
26 changes: 26 additions & 0 deletions tests/baselines/reference/narrowingTypeof.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/narrowingTypeof.ts ===
type __String = (string & { __escapedIdentifier: void }) | (void & { __escapedIdentifier: void });
>__String : Symbol(__String, Decl(narrowingTypeof.ts, 0, 0))
>__escapedIdentifier : Symbol(__escapedIdentifier, Decl(narrowingTypeof.ts, 0, 27))
>__escapedIdentifier : Symbol(__escapedIdentifier, Decl(narrowingTypeof.ts, 0, 68))

declare const s: __String;
>s : Symbol(s, Decl(narrowingTypeof.ts, 2, 13))
>__String : Symbol(__String, Decl(narrowingTypeof.ts, 0, 0))

declare let t: string | number | undefined;
>t : Symbol(t, Decl(narrowingTypeof.ts, 3, 11))

declare function assert(e: unknown): asserts e;
>assert : Symbol(assert, Decl(narrowingTypeof.ts, 3, 43))
>e : Symbol(e, Decl(narrowingTypeof.ts, 5, 24))
>e : Symbol(e, Decl(narrowingTypeof.ts, 5, 24))

assert(typeof s === "string");
>assert : Symbol(assert, Decl(narrowingTypeof.ts, 3, 43))
>s : Symbol(s, Decl(narrowingTypeof.ts, 2, 13))

t = s;
>t : Symbol(t, Decl(narrowingTypeof.ts, 3, 11))
>s : Symbol(s, Decl(narrowingTypeof.ts, 2, 13))

29 changes: 29 additions & 0 deletions tests/baselines/reference/narrowingTypeof.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/narrowingTypeof.ts ===
type __String = (string & { __escapedIdentifier: void }) | (void & { __escapedIdentifier: void });
>__String : __String
>__escapedIdentifier : void
>__escapedIdentifier : void

declare const s: __String;
>s : __String

declare let t: string | number | undefined;
>t : string | number

declare function assert(e: unknown): asserts e;
>assert : (e: unknown) => asserts e
>e : unknown

assert(typeof s === "string");
>assert(typeof s === "string") : void
>assert : (e: unknown) => asserts e
>typeof s === "string" : boolean
>typeof s : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>s : __String
>"string" : "string"

t = s;
>t = s : string & { __escapedIdentifier: void; }
>t : string | number
>s : string & { __escapedIdentifier: void; }

27 changes: 27 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [narrowingTypeofFunction.ts]
type Meta = { foo: string }
interface F { (): string }

const x = (a: (F & Meta) | string) => {
if (typeof a === "function") {
// ts.version >= 4.3.5: never -- unexpected
// ts.version <= 4.2.3: F & Meta -- expected
a;
}
else {
a;
}
}

//// [narrowingTypeofFunction.js]
"use strict";
var x = function (a) {
if (typeof a === "function") {
// ts.version >= 4.3.5: never -- unexpected
// ts.version <= 4.2.3: F & Meta -- expected
a;
}
else {
a;
}
};
27 changes: 27 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/narrowingTypeofFunction.ts ===
type Meta = { foo: string }
>Meta : Symbol(Meta, Decl(narrowingTypeofFunction.ts, 0, 0))
>foo : Symbol(foo, Decl(narrowingTypeofFunction.ts, 0, 13))

interface F { (): string }
>F : Symbol(F, Decl(narrowingTypeofFunction.ts, 0, 27))

const x = (a: (F & Meta) | string) => {
>x : Symbol(x, Decl(narrowingTypeofFunction.ts, 3, 5))
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 11))
>F : Symbol(F, Decl(narrowingTypeofFunction.ts, 0, 27))
>Meta : Symbol(Meta, Decl(narrowingTypeofFunction.ts, 0, 0))

if (typeof a === "function") {
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 11))

// ts.version >= 4.3.5: never -- unexpected
// ts.version <= 4.2.3: F & Meta -- expected
a;
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 11))
}
else {
a;
>a : Symbol(a, Decl(narrowingTypeofFunction.ts, 3, 11))
}
}
28 changes: 28 additions & 0 deletions tests/baselines/reference/narrowingTypeofFunction.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
=== tests/cases/compiler/narrowingTypeofFunction.ts ===
type Meta = { foo: string }
>Meta : Meta
>foo : string

interface F { (): string }

const x = (a: (F & Meta) | string) => {
>x : (a: (F & Meta) | string) => void
>(a: (F & Meta) | string) => { if (typeof a === "function") { // ts.version >= 4.3.5: never -- unexpected // ts.version <= 4.2.3: F & Meta -- expected a; } else { a; }} : (a: (F & Meta) | string) => void
>a : string | (F & Meta)

if (typeof a === "function") {
>typeof a === "function" : boolean
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>a : string | (F & Meta)
>"function" : "function"

// ts.version >= 4.3.5: never -- unexpected
// ts.version <= 4.2.3: F & Meta -- expected
a;
>a : F & Meta
}
else {
a;
>a : string
}
}
10 changes: 10 additions & 0 deletions tests/cases/compiler/narrowingTypeof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

type __String = (string & { __escapedIdentifier: void }) | (void & { __escapedIdentifier: void });

declare const s: __String;
declare let t: string | number | undefined;

declare function assert(e: unknown): asserts e;

assert(typeof s === "string");
t = s;
15 changes: 15 additions & 0 deletions tests/cases/compiler/narrowingTypeofFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true

type Meta = { foo: string }
interface F { (): string }

const x = (a: (F & Meta) | string) => {
if (typeof a === "function") {
// ts.version >= 4.3.5: never -- unexpected
// ts.version <= 4.2.3: F & Meta -- expected
a;
}
else {
a;
}
}