Skip to content
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

Type deduction using mapped types and generics #19211

Closed
k34iml0ch opened this issue Oct 16, 2017 · 2 comments
Closed

Type deduction using mapped types and generics #19211

k34iml0ch opened this issue Oct 16, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@k34iml0ch
Copy link

k34iml0ch commented Oct 16, 2017

TypeScript Version: 2.5.3, 2.6.0rc

Please consider the following simple type definitions.

type PropTypes = {
    'prop1': number,
    'prop2': string
}

interface Handler<T> {        
    handle(value: T): T;
}

type HandlerMap = {
    [K in keyof PropTypes]: Handler<PropTypes[K]>
}

let map: HandlerMap;

The following is not working. Typescript cannot deduce the actual type of the handler.
Listing 1:

function main1(props: PropTypes) {
    Object.keys(props).forEach((key: keyof PropTypes) => {
        map[key].handle(props[key]); // <--  Cannot invoke an expression whose type lacks a call signature...       
    });
}

main1({
    'prop1': 42 ,
    'prop2': 'Hello World' 
})

The followings works.
Listing 2:

function main2(props: PropTypes) {
    Object.keys(props).forEach((key: keyof PropTypes) => {
        let handler = map[key] as Handler<{}> // <-- Works fine using a "cast"
        handler.handle(props[key]);        
    });
}

main2({
    'prop1': 42 ,
    'prop2': 'Hello World' 
})

Listing 3:

function main3(props: PropTypes) {
    Object.keys(props).forEach((key: keyof PropTypes) => {
        helper(map[key], props[key]);  // <-- Works fine using a "helper"
    });
}

function helper<K extends keyof PropTypes>(handler: Handler<PropTypes[K]>, value: PropTypes[K]) {
    handler.handle(value);
}

main3({
    'prop1': 42,
    'prop2': 'Hello World'
})

Expected behavior:

Code in Listing 1 should compile without error

Actual behavior:

Cannot invoke an expression whose type lacks a call signature. Type '((value: number) => number) | ((value: string) => string)' has no compatible call signatures.

link to playground

@mhegazy
Copy link
Contributor

mhegazy commented Oct 25, 2017

Duplicate of #7294

@mhegazy mhegazy marked this as a duplicate of #7294 Oct 25, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 25, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Nov 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants