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 of awaited expression is not inferred correctly, works on 2.5.3 #19843

Closed
RafaelSalguero opened this issue Nov 8, 2017 · 2 comments
Closed
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@RafaelSalguero
Copy link

RafaelSalguero commented Nov 8, 2017

TypeScript Version: 2.6.1
also tested and fails on 2.7.0
also tested and fails on 2.7.0-dev.20180119 (@next)

Code

type MyType<T> = {[K in keyof T]: string | true |  PromiseLike<string | true>  };
function MapObject<T, TOut>(value: T, map: <K extends keyof T>(value: T[K], key: K) => TOut) : {[K in keyof T] : TOut} {
    return null as any;
}

//MyFunction
//2.5.3 compiles
//2.6.1 do not compiles since ret variable is not compatible with the function return value
//@next do not compiles since ret variable is not compatible with the function return value
function MyFunction<T>(value: MyType<T>) : {[K in keyof T] : Promise<string | undefined>} {
    const a = MapObject(value, x => x);

    //B should be { [K in keyof T ]: Promise<string | undefined>}
    const b = MapObject(a, async x => {
        //myVal type:
        //(correct)   2.5.3: string | true
        //(incorrect) 2.6.1: { [K in keyof T]: MyType<T>[any] }[K]
        //(incorrect) @next: { [K in keyof T]: MyType<T>[any] }[K]
        const myVal = await x;
        
        //ret type should be string | undefined
        //(correct)   2.5.3: string | undefined 
        //(incorrect) 2.6.1: { [K in keyof T]: MyType<T>[any] }[K] | undefined
        //(incorrect) @next: { [K in keyof T]: MyType<T>[any] }[K] | undefined
        const ret = myVal === true ? undefined : myVal;
        
        return ret;
    })

    return b;
}

Expected behavior:
myVal variable inferred type to be string | true
ret variable inferred type to be string | undefined
The code should compile since the return value of the function type and the return expression is the same

Actual behavior:
myVal variable inferred type is { [K in keyof T]: MyType<T>[any] }[K]
ret variable inferred type is { [K in keyof T]: MyType<T>[any] }[K] | undefined
The code does not compile since the return type does not match the return expression

@mhegazy
Copy link
Contributor

mhegazy commented Jul 18, 2018

That is another instance for awaited higher order type operator. #17077 and #21613 attempted to get that done, but eventually failed.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 18, 2018
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants