mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
56 lines
3.3 KiB
Plaintext
56 lines
3.3 KiB
Plaintext
//// [tests/cases/compiler/genericCapturingFunctionNarrowing.ts] ////
|
|
|
|
=== genericCapturingFunctionNarrowing.ts ===
|
|
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
|
|
>needsToNarrowTheType : Symbol(needsToNarrowTheType, Decl(genericCapturingFunctionNarrowing.ts, 0, 0))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
|
|
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
|
|
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
|
|
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
>other : Symbol(other, Decl(genericCapturingFunctionNarrowing.ts, 0, 147))
|
|
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
|
|
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
|
|
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
|
|
|
|
if (hasAFoo(thing)) {
|
|
>hasAFoo : Symbol(hasAFoo, Decl(genericCapturingFunctionNarrowing.ts, 7, 5))
|
|
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
|
|
|
|
console.log(thing.foo);
|
|
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
|
|
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
|
|
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
|
|
>thing.foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
|
|
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
|
|
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
|
|
}
|
|
else {
|
|
// I would expect this to work because the type should be narrowed in this branch to `Second`
|
|
console.log(thing.bar); // Error: Property 'bar' does not exist on type 'First | Second'.
|
|
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
|
|
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
|
|
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
|
|
>thing.bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
|
|
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
|
|
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
|
|
}
|
|
|
|
function hasAFoo(value: First | Second): value is First {
|
|
>hasAFoo : Symbol(hasAFoo, Decl(genericCapturingFunctionNarrowing.ts, 7, 5))
|
|
>value : Symbol(value, Decl(genericCapturingFunctionNarrowing.ts, 9, 21))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
|
|
>value : Symbol(value, Decl(genericCapturingFunctionNarrowing.ts, 9, 21))
|
|
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
|
|
|
|
return "foo" in value;
|
|
>value : Symbol(value, Decl(genericCapturingFunctionNarrowing.ts, 9, 21))
|
|
}
|
|
}
|