mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Add test for intersection narrowing bug (#45296)
This commit is contained in:
40
tests/baselines/reference/narrowingIntersection.js
Normal file
40
tests/baselines/reference/narrowingIntersection.js
Normal file
@@ -0,0 +1,40 @@
|
||||
//// [narrowingIntersection.ts]
|
||||
// Somehow this being an intersection matters.
|
||||
type FooAndBaz = { foo: unknown } & { baz: unknown };
|
||||
|
||||
type Disjoint =
|
||||
| { readonly value: string; readonly err?: never; }
|
||||
| { readonly value?: never; readonly err: FooAndBaz; };
|
||||
|
||||
function test1(result: Disjoint): string {
|
||||
if (result.err) {
|
||||
throw result.err;
|
||||
}
|
||||
// Error, should OK
|
||||
return result.value;
|
||||
}
|
||||
|
||||
type TrivialIntersection = { a: 1 } & { a: 1 };
|
||||
|
||||
function want0(x: 0) {}
|
||||
|
||||
function test2(a: 0 | TrivialIntersection) {
|
||||
if (a === 0) {
|
||||
want0(a); // Fails, but expect to work
|
||||
}
|
||||
}
|
||||
|
||||
//// [narrowingIntersection.js]
|
||||
function test1(result) {
|
||||
if (result.err) {
|
||||
throw result.err;
|
||||
}
|
||||
// Error, should OK
|
||||
return result.value;
|
||||
}
|
||||
function want0(x) { }
|
||||
function test2(a) {
|
||||
if (a === 0) {
|
||||
want0(a); // Fails, but expect to work
|
||||
}
|
||||
}
|
||||
63
tests/baselines/reference/narrowingIntersection.symbols
Normal file
63
tests/baselines/reference/narrowingIntersection.symbols
Normal file
@@ -0,0 +1,63 @@
|
||||
=== tests/cases/compiler/narrowingIntersection.ts ===
|
||||
// Somehow this being an intersection matters.
|
||||
type FooAndBaz = { foo: unknown } & { baz: unknown };
|
||||
>FooAndBaz : Symbol(FooAndBaz, Decl(narrowingIntersection.ts, 0, 0))
|
||||
>foo : Symbol(foo, Decl(narrowingIntersection.ts, 1, 18))
|
||||
>baz : Symbol(baz, Decl(narrowingIntersection.ts, 1, 37))
|
||||
|
||||
type Disjoint =
|
||||
>Disjoint : Symbol(Disjoint, Decl(narrowingIntersection.ts, 1, 53))
|
||||
|
||||
| { readonly value: string; readonly err?: never; }
|
||||
>value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4))
|
||||
>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28))
|
||||
|
||||
| { readonly value?: never; readonly err: FooAndBaz; };
|
||||
>value : Symbol(value, Decl(narrowingIntersection.ts, 5, 4))
|
||||
>err : Symbol(err, Decl(narrowingIntersection.ts, 5, 28))
|
||||
>FooAndBaz : Symbol(FooAndBaz, Decl(narrowingIntersection.ts, 0, 0))
|
||||
|
||||
function test1(result: Disjoint): string {
|
||||
>test1 : Symbol(test1, Decl(narrowingIntersection.ts, 5, 56))
|
||||
>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15))
|
||||
>Disjoint : Symbol(Disjoint, Decl(narrowingIntersection.ts, 1, 53))
|
||||
|
||||
if (result.err) {
|
||||
>result.err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28))
|
||||
>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15))
|
||||
>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28))
|
||||
|
||||
throw result.err;
|
||||
>result.err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28))
|
||||
>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15))
|
||||
>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28))
|
||||
}
|
||||
// Error, should OK
|
||||
return result.value;
|
||||
>result.value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4), Decl(narrowingIntersection.ts, 5, 4))
|
||||
>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15))
|
||||
>value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4), Decl(narrowingIntersection.ts, 5, 4))
|
||||
}
|
||||
|
||||
type TrivialIntersection = { a: 1 } & { a: 1 };
|
||||
>TrivialIntersection : Symbol(TrivialIntersection, Decl(narrowingIntersection.ts, 13, 1))
|
||||
>a : Symbol(a, Decl(narrowingIntersection.ts, 15, 28))
|
||||
>a : Symbol(a, Decl(narrowingIntersection.ts, 15, 39))
|
||||
|
||||
function want0(x: 0) {}
|
||||
>want0 : Symbol(want0, Decl(narrowingIntersection.ts, 15, 47))
|
||||
>x : Symbol(x, Decl(narrowingIntersection.ts, 17, 15))
|
||||
|
||||
function test2(a: 0 | TrivialIntersection) {
|
||||
>test2 : Symbol(test2, Decl(narrowingIntersection.ts, 17, 23))
|
||||
>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15))
|
||||
>TrivialIntersection : Symbol(TrivialIntersection, Decl(narrowingIntersection.ts, 13, 1))
|
||||
|
||||
if (a === 0) {
|
||||
>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15))
|
||||
|
||||
want0(a); // Fails, but expect to work
|
||||
>want0 : Symbol(want0, Decl(narrowingIntersection.ts, 15, 47))
|
||||
>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15))
|
||||
}
|
||||
}
|
||||
63
tests/baselines/reference/narrowingIntersection.types
Normal file
63
tests/baselines/reference/narrowingIntersection.types
Normal file
@@ -0,0 +1,63 @@
|
||||
=== tests/cases/compiler/narrowingIntersection.ts ===
|
||||
// Somehow this being an intersection matters.
|
||||
type FooAndBaz = { foo: unknown } & { baz: unknown };
|
||||
>FooAndBaz : FooAndBaz
|
||||
>foo : unknown
|
||||
>baz : unknown
|
||||
|
||||
type Disjoint =
|
||||
>Disjoint : Disjoint
|
||||
|
||||
| { readonly value: string; readonly err?: never; }
|
||||
>value : string
|
||||
>err : never
|
||||
|
||||
| { readonly value?: never; readonly err: FooAndBaz; };
|
||||
>value : never
|
||||
>err : FooAndBaz
|
||||
|
||||
function test1(result: Disjoint): string {
|
||||
>test1 : (result: Disjoint) => string
|
||||
>result : Disjoint
|
||||
|
||||
if (result.err) {
|
||||
>result.err : FooAndBaz
|
||||
>result : Disjoint
|
||||
>err : FooAndBaz
|
||||
|
||||
throw result.err;
|
||||
>result.err : FooAndBaz
|
||||
>result : Disjoint
|
||||
>err : FooAndBaz
|
||||
}
|
||||
// Error, should OK
|
||||
return result.value;
|
||||
>result.value : string
|
||||
>result : Disjoint
|
||||
>value : string
|
||||
}
|
||||
|
||||
type TrivialIntersection = { a: 1 } & { a: 1 };
|
||||
>TrivialIntersection : TrivialIntersection
|
||||
>a : 1
|
||||
>a : 1
|
||||
|
||||
function want0(x: 0) {}
|
||||
>want0 : (x: 0) => void
|
||||
>x : 0
|
||||
|
||||
function test2(a: 0 | TrivialIntersection) {
|
||||
>test2 : (a: 0 | TrivialIntersection) => void
|
||||
>a : 0 | TrivialIntersection
|
||||
|
||||
if (a === 0) {
|
||||
>a === 0 : boolean
|
||||
>a : 0 | TrivialIntersection
|
||||
>0 : 0
|
||||
|
||||
want0(a); // Fails, but expect to work
|
||||
>want0(a) : void
|
||||
>want0 : (x: 0) => void
|
||||
>a : 0
|
||||
}
|
||||
}
|
||||
24
tests/cases/compiler/narrowingIntersection.ts
Normal file
24
tests/cases/compiler/narrowingIntersection.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
// Somehow this being an intersection matters.
|
||||
type FooAndBaz = { foo: unknown } & { baz: unknown };
|
||||
|
||||
type Disjoint =
|
||||
| { readonly value: string; readonly err?: never; }
|
||||
| { readonly value?: never; readonly err: FooAndBaz; };
|
||||
|
||||
function test1(result: Disjoint): string {
|
||||
if (result.err) {
|
||||
throw result.err;
|
||||
}
|
||||
// Error, should OK
|
||||
return result.value;
|
||||
}
|
||||
|
||||
type TrivialIntersection = { a: 1 } & { a: 1 };
|
||||
|
||||
function want0(x: 0) {}
|
||||
|
||||
function test2(a: 0 | TrivialIntersection) {
|
||||
if (a === 0) {
|
||||
want0(a); // Fails, but expect to work
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user