Skip to content

Improve prop type key when mapping over tuple #48599

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

Closed
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17034,7 +17034,7 @@ namespace ts {
function instantiateMappedTupleType(tupleType: TupleTypeReference, mappedType: MappedType, mapper: TypeMapper) {
const elementFlags = tupleType.target.elementFlags;
const elementTypes = map(getTypeArguments(tupleType), (_, i) =>
instantiateMappedTypeTemplate(mappedType, getStringLiteralType("" + i), !!(elementFlags[i] & ElementFlags.Optional), mapper));
instantiateMappedTypeTemplate(mappedType, getNumberLiteralType(i), !!(elementFlags[i] & ElementFlags.Optional), mapper));
const modifiers = getMappedTypeModifiers(mappedType);
const newTupleModifiers = modifiers & MappedTypeModifiers.IncludeOptional ? map(elementFlags, f => f & ElementFlags.Required ? ElementFlags.Optional : f) :
modifiers & MappedTypeModifiers.ExcludeOptional ? map(elementFlags, f => f & ElementFlags.Optional ? ElementFlags.Required : f) :
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/mappedTypeTupleTypePropType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [mappedTypeTupleTypePropType.ts]
type Indices<T extends Array<any>> = {
[K in keyof T]: K
};

// should contain numbers
type MyIndices = Indices<[string, number]>;
// union of indices should be preserved
type StillMyIndices = MyIndices[number] & number

// simplified repro from https://twitter.com/oleg008/status/1508422774401949704

type Container<V> = {
value: V;
};

type UnwrapContainers<T extends Container<unknown>[]> = {
[K in keyof T]: T[K & number]["value"];
};

declare function combine<T extends Container<unknown>[]>(
containers: [...T],
callback: (...values: UnwrapContainers<T>) => void
): void;

declare const container1: Container<string>;
declare const container2: Container<number>;

combine([container1, container2], (value1, value2) => {
const val1: string = value1;
const val2: number = value2;
});



//// [mappedTypeTupleTypePropType.js]
combine([container1, container2], function (value1, value2) {
var val1 = value1;
var val2 = value2;
});
91 changes: 91 additions & 0 deletions tests/baselines/reference/mappedTypeTupleTypePropType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
=== tests/cases/compiler/mappedTypeTupleTypePropType.ts ===
type Indices<T extends Array<any>> = {
>Indices : Symbol(Indices, Decl(mappedTypeTupleTypePropType.ts, 0, 0))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 0, 13))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

[K in keyof T]: K
>K : Symbol(K, Decl(mappedTypeTupleTypePropType.ts, 1, 3))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 0, 13))
>K : Symbol(K, Decl(mappedTypeTupleTypePropType.ts, 1, 3))

};

// should contain numbers
type MyIndices = Indices<[string, number]>;
>MyIndices : Symbol(MyIndices, Decl(mappedTypeTupleTypePropType.ts, 2, 2))
>Indices : Symbol(Indices, Decl(mappedTypeTupleTypePropType.ts, 0, 0))

// union of indices should be preserved
type StillMyIndices = MyIndices[number] & number
>StillMyIndices : Symbol(StillMyIndices, Decl(mappedTypeTupleTypePropType.ts, 5, 43))
>MyIndices : Symbol(MyIndices, Decl(mappedTypeTupleTypePropType.ts, 2, 2))

// simplified repro from https://twitter.com/oleg008/status/1508422774401949704

type Container<V> = {
>Container : Symbol(Container, Decl(mappedTypeTupleTypePropType.ts, 7, 48))
>V : Symbol(V, Decl(mappedTypeTupleTypePropType.ts, 11, 15))

value: V;
>value : Symbol(value, Decl(mappedTypeTupleTypePropType.ts, 11, 21))
>V : Symbol(V, Decl(mappedTypeTupleTypePropType.ts, 11, 15))

};

type UnwrapContainers<T extends Container<unknown>[]> = {
>UnwrapContainers : Symbol(UnwrapContainers, Decl(mappedTypeTupleTypePropType.ts, 13, 2))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 15, 22))
>Container : Symbol(Container, Decl(mappedTypeTupleTypePropType.ts, 7, 48))

[K in keyof T]: T[K & number]["value"];
>K : Symbol(K, Decl(mappedTypeTupleTypePropType.ts, 16, 3))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 15, 22))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 15, 22))
>K : Symbol(K, Decl(mappedTypeTupleTypePropType.ts, 16, 3))

};

declare function combine<T extends Container<unknown>[]>(
>combine : Symbol(combine, Decl(mappedTypeTupleTypePropType.ts, 17, 2))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 19, 25))
>Container : Symbol(Container, Decl(mappedTypeTupleTypePropType.ts, 7, 48))

containers: [...T],
>containers : Symbol(containers, Decl(mappedTypeTupleTypePropType.ts, 19, 57))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 19, 25))

callback: (...values: UnwrapContainers<T>) => void
>callback : Symbol(callback, Decl(mappedTypeTupleTypePropType.ts, 20, 21))
>values : Symbol(values, Decl(mappedTypeTupleTypePropType.ts, 21, 13))
>UnwrapContainers : Symbol(UnwrapContainers, Decl(mappedTypeTupleTypePropType.ts, 13, 2))
>T : Symbol(T, Decl(mappedTypeTupleTypePropType.ts, 19, 25))

): void;

declare const container1: Container<string>;
>container1 : Symbol(container1, Decl(mappedTypeTupleTypePropType.ts, 24, 13))
>Container : Symbol(Container, Decl(mappedTypeTupleTypePropType.ts, 7, 48))

declare const container2: Container<number>;
>container2 : Symbol(container2, Decl(mappedTypeTupleTypePropType.ts, 25, 13))
>Container : Symbol(Container, Decl(mappedTypeTupleTypePropType.ts, 7, 48))

combine([container1, container2], (value1, value2) => {
>combine : Symbol(combine, Decl(mappedTypeTupleTypePropType.ts, 17, 2))
>container1 : Symbol(container1, Decl(mappedTypeTupleTypePropType.ts, 24, 13))
>container2 : Symbol(container2, Decl(mappedTypeTupleTypePropType.ts, 25, 13))
>value1 : Symbol(value1, Decl(mappedTypeTupleTypePropType.ts, 27, 35))
>value2 : Symbol(value2, Decl(mappedTypeTupleTypePropType.ts, 27, 42))

const val1: string = value1;
>val1 : Symbol(val1, Decl(mappedTypeTupleTypePropType.ts, 28, 7))
>value1 : Symbol(value1, Decl(mappedTypeTupleTypePropType.ts, 27, 35))

const val2: number = value2;
>val2 : Symbol(val2, Decl(mappedTypeTupleTypePropType.ts, 29, 7))
>value2 : Symbol(value2, Decl(mappedTypeTupleTypePropType.ts, 27, 42))

});


70 changes: 70 additions & 0 deletions tests/baselines/reference/mappedTypeTupleTypePropType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
=== tests/cases/compiler/mappedTypeTupleTypePropType.ts ===
type Indices<T extends Array<any>> = {
>Indices : Indices<T>

[K in keyof T]: K
};

// should contain numbers
type MyIndices = Indices<[string, number]>;
>MyIndices : [0, 1]

// union of indices should be preserved
type StillMyIndices = MyIndices[number] & number
>StillMyIndices : StillMyIndices

// simplified repro from https://twitter.com/oleg008/status/1508422774401949704

type Container<V> = {
>Container : Container<V>

value: V;
>value : V

};

type UnwrapContainers<T extends Container<unknown>[]> = {
>UnwrapContainers : UnwrapContainers<T>

[K in keyof T]: T[K & number]["value"];
};

declare function combine<T extends Container<unknown>[]>(
>combine : <T extends Container<unknown>[]>(containers: [...T], callback: (...values: UnwrapContainers<T>) => void) => void

containers: [...T],
>containers : [...T]

callback: (...values: UnwrapContainers<T>) => void
>callback : (...values: UnwrapContainers<T>) => void
>values : UnwrapContainers<T>

): void;

declare const container1: Container<string>;
>container1 : Container<string>

declare const container2: Container<number>;
>container2 : Container<number>

combine([container1, container2], (value1, value2) => {
>combine([container1, container2], (value1, value2) => { const val1: string = value1; const val2: number = value2;}) : void
>combine : <T extends Container<unknown>[]>(containers: [...T], callback: (...values: UnwrapContainers<T>) => void) => void
>[container1, container2] : [Container<string>, Container<number>]
>container1 : Container<string>
>container2 : Container<number>
>(value1, value2) => { const val1: string = value1; const val2: number = value2;} : (value1: string, value2: number) => void
>value1 : string
>value2 : number

const val1: string = value1;
>val1 : string
>value1 : string

const val2: number = value2;
>val2 : number
>value2 : number

});


32 changes: 32 additions & 0 deletions tests/cases/compiler/mappedTypeTupleTypePropType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type Indices<T extends Array<any>> = {
[K in keyof T]: K
};

// should contain numbers
type MyIndices = Indices<[string, number]>;
// union of indices should be preserved
type StillMyIndices = MyIndices[number] & number

// simplified repro from https://twitter.com/oleg008/status/1508422774401949704

type Container<V> = {
value: V;
};

type UnwrapContainers<T extends Container<unknown>[]> = {
[K in keyof T]: T[K & number]["value"];
};

declare function combine<T extends Container<unknown>[]>(
containers: [...T],
callback: (...values: UnwrapContainers<T>) => void
): void;

declare const container1: Container<string>;
declare const container2: Container<number>;

combine([container1, container2], (value1, value2) => {
const val1: string = value1;
const val2: number = value2;
});