-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18363 from Microsoft/fixIntersectionInference
Fix intersection inference
- Loading branch information
Showing
7 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// [intersectionTypeInference2.ts] | ||
declare function f<T>(x: { prop: T }): T; | ||
|
||
declare const a: { prop: string } & { prop: number }; | ||
declare const b: { prop: string & number }; | ||
|
||
f(a); // string & number | ||
f(b); // string & number | ||
|
||
// Repro from #18354 | ||
|
||
declare function f2<T, Key extends keyof T>(obj: {[K in keyof T]: T[K]}, key: Key): T[Key]; | ||
|
||
declare const obj: { a: string } & { b: string }; | ||
f2(obj, 'a'); | ||
f2(obj, 'b'); | ||
|
||
|
||
//// [intersectionTypeInference2.js] | ||
f(a); // string & number | ||
f(b); // string & number | ||
f2(obj, 'a'); | ||
f2(obj, 'b'); |
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/intersectionTypeInference2.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
=== tests/cases/conformance/types/intersection/intersectionTypeInference2.ts === | ||
declare function f<T>(x: { prop: T }): T; | ||
>f : Symbol(f, Decl(intersectionTypeInference2.ts, 0, 0)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 0, 19)) | ||
>x : Symbol(x, Decl(intersectionTypeInference2.ts, 0, 22)) | ||
>prop : Symbol(prop, Decl(intersectionTypeInference2.ts, 0, 26)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 0, 19)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 0, 19)) | ||
|
||
declare const a: { prop: string } & { prop: number }; | ||
>a : Symbol(a, Decl(intersectionTypeInference2.ts, 2, 13)) | ||
>prop : Symbol(prop, Decl(intersectionTypeInference2.ts, 2, 18)) | ||
>prop : Symbol(prop, Decl(intersectionTypeInference2.ts, 2, 37)) | ||
|
||
declare const b: { prop: string & number }; | ||
>b : Symbol(b, Decl(intersectionTypeInference2.ts, 3, 13)) | ||
>prop : Symbol(prop, Decl(intersectionTypeInference2.ts, 3, 18)) | ||
|
||
f(a); // string & number | ||
>f : Symbol(f, Decl(intersectionTypeInference2.ts, 0, 0)) | ||
>a : Symbol(a, Decl(intersectionTypeInference2.ts, 2, 13)) | ||
|
||
f(b); // string & number | ||
>f : Symbol(f, Decl(intersectionTypeInference2.ts, 0, 0)) | ||
>b : Symbol(b, Decl(intersectionTypeInference2.ts, 3, 13)) | ||
|
||
// Repro from #18354 | ||
|
||
declare function f2<T, Key extends keyof T>(obj: {[K in keyof T]: T[K]}, key: Key): T[Key]; | ||
>f2 : Symbol(f2, Decl(intersectionTypeInference2.ts, 6, 5)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 10, 20)) | ||
>Key : Symbol(Key, Decl(intersectionTypeInference2.ts, 10, 22)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 10, 20)) | ||
>obj : Symbol(obj, Decl(intersectionTypeInference2.ts, 10, 44)) | ||
>K : Symbol(K, Decl(intersectionTypeInference2.ts, 10, 51)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 10, 20)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 10, 20)) | ||
>K : Symbol(K, Decl(intersectionTypeInference2.ts, 10, 51)) | ||
>key : Symbol(key, Decl(intersectionTypeInference2.ts, 10, 72)) | ||
>Key : Symbol(Key, Decl(intersectionTypeInference2.ts, 10, 22)) | ||
>T : Symbol(T, Decl(intersectionTypeInference2.ts, 10, 20)) | ||
>Key : Symbol(Key, Decl(intersectionTypeInference2.ts, 10, 22)) | ||
|
||
declare const obj: { a: string } & { b: string }; | ||
>obj : Symbol(obj, Decl(intersectionTypeInference2.ts, 12, 13)) | ||
>a : Symbol(a, Decl(intersectionTypeInference2.ts, 12, 20)) | ||
>b : Symbol(b, Decl(intersectionTypeInference2.ts, 12, 36)) | ||
|
||
f2(obj, 'a'); | ||
>f2 : Symbol(f2, Decl(intersectionTypeInference2.ts, 6, 5)) | ||
>obj : Symbol(obj, Decl(intersectionTypeInference2.ts, 12, 13)) | ||
|
||
f2(obj, 'b'); | ||
>f2 : Symbol(f2, Decl(intersectionTypeInference2.ts, 6, 5)) | ||
>obj : Symbol(obj, Decl(intersectionTypeInference2.ts, 12, 13)) | ||
|
62 changes: 62 additions & 0 deletions
62
tests/baselines/reference/intersectionTypeInference2.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
=== tests/cases/conformance/types/intersection/intersectionTypeInference2.ts === | ||
declare function f<T>(x: { prop: T }): T; | ||
>f : <T>(x: { prop: T; }) => T | ||
>T : T | ||
>x : { prop: T; } | ||
>prop : T | ||
>T : T | ||
>T : T | ||
|
||
declare const a: { prop: string } & { prop: number }; | ||
>a : { prop: string; } & { prop: number; } | ||
>prop : string | ||
>prop : number | ||
|
||
declare const b: { prop: string & number }; | ||
>b : { prop: string & number; } | ||
>prop : string & number | ||
|
||
f(a); // string & number | ||
>f(a) : string & number | ||
>f : <T>(x: { prop: T; }) => T | ||
>a : { prop: string; } & { prop: number; } | ||
|
||
f(b); // string & number | ||
>f(b) : string & number | ||
>f : <T>(x: { prop: T; }) => T | ||
>b : { prop: string & number; } | ||
|
||
// Repro from #18354 | ||
|
||
declare function f2<T, Key extends keyof T>(obj: {[K in keyof T]: T[K]}, key: Key): T[Key]; | ||
>f2 : <T, Key extends keyof T>(obj: { [K in keyof T]: T[K]; }, key: Key) => T[Key] | ||
>T : T | ||
>Key : Key | ||
>T : T | ||
>obj : { [K in keyof T]: T[K]; } | ||
>K : K | ||
>T : T | ||
>T : T | ||
>K : K | ||
>key : Key | ||
>Key : Key | ||
>T : T | ||
>Key : Key | ||
|
||
declare const obj: { a: string } & { b: string }; | ||
>obj : { a: string; } & { b: string; } | ||
>a : string | ||
>b : string | ||
|
||
f2(obj, 'a'); | ||
>f2(obj, 'a') : string | ||
>f2 : <T, Key extends keyof T>(obj: { [K in keyof T]: T[K]; }, key: Key) => T[Key] | ||
>obj : { a: string; } & { b: string; } | ||
>'a' : "a" | ||
|
||
f2(obj, 'b'); | ||
>f2(obj, 'b') : string | ||
>f2 : <T, Key extends keyof T>(obj: { [K in keyof T]: T[K]; }, key: Key) => T[Key] | ||
>obj : { a: string; } & { b: string; } | ||
>'b' : "b" | ||
|
15 changes: 15 additions & 0 deletions
15
tests/cases/conformance/types/intersection/intersectionTypeInference2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare function f<T>(x: { prop: T }): T; | ||
|
||
declare const a: { prop: string } & { prop: number }; | ||
declare const b: { prop: string & number }; | ||
|
||
f(a); // string & number | ||
f(b); // string & number | ||
|
||
// Repro from #18354 | ||
|
||
declare function f2<T, Key extends keyof T>(obj: {[K in keyof T]: T[K]}, key: Key): T[Key]; | ||
|
||
declare const obj: { a: string } & { b: string }; | ||
f2(obj, 'a'); | ||
f2(obj, 'b'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters