Skip to content

Type of awaited expression is not inferred correctly, works on 2.5.3 #19843

Closed
@RafaelSalguero

Description

@RafaelSalguero

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions