-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Mapped generic type loses types during inference #56861
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
Comments
That gets you to " class Decoder<T> {
declare prop: T; // you need a structural dependency
}
class TupleDecoder<const TupleType extends ReadonlyArray<any>> extends Decoder<TupleType> {
// not number -----> ^^^
constructor(entryDecoders: [...{ [K in keyof TupleType]: Decoder<TupleType[K]> }]) {
// ^^^^ <-- variadic tuple to hint you want a tuple --->^
super();
}
}
declare const decoder1: Decoder<number>;
declare const decoder2: Decoder<string>;
const tupleDecoder = new TupleDecoder([decoder1, decoder2]);
// const tupleDecoder: TupleDecoder<[number, string]> To recap, the only problem I see here is the one that looks like a duplicate of #53813. Everything else behaves as expected. |
And with #56555 you won't even have to "hint" that you want a tuple here - since the usage of a mapped type will already be treated as such a hint: TS playground |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
mapped tuple type, const inference
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ssl=13&ssc=1&pln=1&pc=1#code/MYGwhgzhAEAiCmwD2ATeAnAPAFQHzQG9oBfAKFNEhmwFcAHEeBZNLZAOwgBdpaH5sATzrxo8AB5d47FDABK8MCiTsQggILp0YQZnY0AtgCMMufBKkyYzVBhz1GQkfgKlo76B27oawLknQACmkudEEbVggALkJoAG0AaWgAS3ZoAGt4QSQAM14HAWF4AF0YiLs+RyLE4vxiAEpCNw8WiHoMQPqAbmb3MjJSNEp0US8eIdt0AEYyxEm9QxN0XB6h8BHPFW5oCdYAJlmWO29UgHMV0gB6S+gASXYcjBGUaH98-nKsfWMMONqKLY8LgFT7QAC80HY8AA7u9GJ9AnFdhgpgAaHZzfbFbqkIA
π» Code
π Actual behavior
The type of
tupleDecoder
is inferred toTupleDecoder<number[]>
π Expected behavior
It should be inferred to be
TupleDecoder<readonly [number, string]>
.Additional information about the issue
I believe this problem is probably two seperate problems.
One is that
const ... extends ReadonlyArray<any>
isn't being considered for mapped types.Second is that inference is not picking up the string type in the array due to the mapped type.
The text was updated successfully, but these errors were encountered: