Skip to content

Mapped generic type loses types during inferenceΒ #56861

Closed as not planned
Closed as not planned
@Jamesernator

Description

@Jamesernator

πŸ”Ž Search Terms

mapped tuple type, const inference

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mapped types

⏯ Playground Link

https://www.typescriptlang.org/play?ssl=13&ssc=1&pln=1&pc=1#code/MYGwhgzhAEAiCmwD2ATeAnAPAFQHzQG9oBfAKFNEhmwFcAHEeBZNLZAOwgBdpaH5sATzrxo8AB5d47FDABK8MCiTsQggILp0YQZnY0AtgCMMufBKkyYzVBhz1GQkfgKlo76B27oawLknQACmkudEEbVggALkJoAG0AaWgAS3ZoAGt4QSQAM14HAWF4AF0YiLs+RyLE4vxiAEpCNw8WiHoMQPqAbmb3MjJSNEp0US8eIdt0AEYyxEm9QxN0XB6h8BHPFW5oCdYAJlmWO29UgHMV0gB6S+gASXYcjBGUaH98-nKsfWMMONqKLY8LgFT7QAC80HY8AA7u9GJ9AnFdhgpgAaHZzfbFbqkIA

πŸ’» Code

class Decoder<T> { }

class TupleDecoder<const TupleType extends ReadonlyArray<number>> extends Decoder<TupleType> {
    constructor(entryDecoders: { [K in keyof TupleType]: Decoder<TupleType[K]> }) {
        super();
    }
}

declare const decoder1: Decoder<number>;
declare const decoder2: Decoder<string>;
// Inferred to TupleDecoder<number[]>
const tupleDecoder = new TupleDecoder([decoder1, decoder2]);

πŸ™ Actual behavior

The type of tupleDecoder is inferred to TupleDecoder<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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions