Skip to content

Map over unions when getting iterated element type from contextual type #40592

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

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23651,7 +23651,10 @@ namespace ts {
function getContextualTypeForElementExpression(arrayContextualType: Type | undefined, index: number): Type | undefined {
return arrayContextualType && (
getTypeOfPropertyOfContextualType(arrayContextualType, "" + index as __String)
|| getIteratedTypeOrElementType(IterationUse.Element, arrayContextualType, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false));
|| mapType(
arrayContextualType,
t => getIteratedTypeOrElementType(IterationUse.Element, t, undefinedType, /*errorNode*/ undefined, /*checkAssignability*/ false),
/*noReductions*/ true));
}

// In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
Expand Down
17 changes: 17 additions & 0 deletions tests/baselines/reference/contextualTypeIterableUnions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [contextualTypeIterableUnions.ts]
declare class DMap<K, V> {
constructor(iterable: Iterable<[K, V]> | undefined);
}
new DMap([["1", 2]]);

const i1: Iterable<{ a: true }> | undefined = [{ a: true }];
const i2: Iterable<{ a: true }> | Iterable<{ b: false }> = [{ b: false }];
const i3: Iterable<number> | 1[] = [2];


//// [contextualTypeIterableUnions.js]
"use strict";
new DMap([["1", 2]]);
const i1 = [{ a: true }];
const i2 = [{ b: false }];
const i3 = [2];
33 changes: 33 additions & 0 deletions tests/baselines/reference/contextualTypeIterableUnions.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/contextualTypeIterableUnions.ts ===
declare class DMap<K, V> {
>DMap : Symbol(DMap, Decl(contextualTypeIterableUnions.ts, 0, 0))
>K : Symbol(K, Decl(contextualTypeIterableUnions.ts, 0, 19))
>V : Symbol(V, Decl(contextualTypeIterableUnions.ts, 0, 21))

constructor(iterable: Iterable<[K, V]> | undefined);
>iterable : Symbol(iterable, Decl(contextualTypeIterableUnions.ts, 1, 14))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>K : Symbol(K, Decl(contextualTypeIterableUnions.ts, 0, 19))
>V : Symbol(V, Decl(contextualTypeIterableUnions.ts, 0, 21))
}
new DMap([["1", 2]]);
>DMap : Symbol(DMap, Decl(contextualTypeIterableUnions.ts, 0, 0))

const i1: Iterable<{ a: true }> | undefined = [{ a: true }];
>i1 : Symbol(i1, Decl(contextualTypeIterableUnions.ts, 5, 5))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>a : Symbol(a, Decl(contextualTypeIterableUnions.ts, 5, 20))
>a : Symbol(a, Decl(contextualTypeIterableUnions.ts, 5, 48))

const i2: Iterable<{ a: true }> | Iterable<{ b: false }> = [{ b: false }];
>i2 : Symbol(i2, Decl(contextualTypeIterableUnions.ts, 6, 5))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>a : Symbol(a, Decl(contextualTypeIterableUnions.ts, 6, 20))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>b : Symbol(b, Decl(contextualTypeIterableUnions.ts, 6, 44))
>b : Symbol(b, Decl(contextualTypeIterableUnions.ts, 6, 61))

const i3: Iterable<number> | 1[] = [2];
>i3 : Symbol(i3, Decl(contextualTypeIterableUnions.ts, 7, 5))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))

40 changes: 40 additions & 0 deletions tests/baselines/reference/contextualTypeIterableUnions.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/contextualTypeIterableUnions.ts ===
declare class DMap<K, V> {
>DMap : DMap<K, V>

constructor(iterable: Iterable<[K, V]> | undefined);
>iterable : Iterable<[K, V]> | undefined
}
new DMap([["1", 2]]);
>new DMap([["1", 2]]) : DMap<string, number>
>DMap : typeof DMap
>[["1", 2]] : [string, number][]
>["1", 2] : [string, number]
>"1" : "1"
>2 : 2

const i1: Iterable<{ a: true }> | undefined = [{ a: true }];
>i1 : Iterable<{ a: true; }> | undefined
>a : true
>true : true
>[{ a: true }] : { a: true; }[]
>{ a: true } : { a: true; }
>a : true
>true : true

const i2: Iterable<{ a: true }> | Iterable<{ b: false }> = [{ b: false }];
>i2 : Iterable<{ a: true; }> | Iterable<{ b: false; }>
>a : true
>true : true
>b : false
>false : false
>[{ b: false }] : { b: false; }[]
>{ b: false } : { b: false; }
>b : false
>false : false

const i3: Iterable<number> | 1[] = [2];
>i3 : Iterable<number> | 1[]
>[2] : 2[]
>2 : 2

11 changes: 11 additions & 0 deletions tests/cases/compiler/contextualTypeIterableUnions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @strict: true
// @target: esnext

declare class DMap<K, V> {
constructor(iterable: Iterable<[K, V]> | undefined);
}
new DMap([["1", 2]]);

const i1: Iterable<{ a: true }> | undefined = [{ a: true }];
const i2: Iterable<{ a: true }> | Iterable<{ b: false }> = [{ b: false }];
const i3: Iterable<number> | 1[] = [2];