Skip to content

Type inference in generic return type #18839

Closed
@Dante-101

Description

@Dante-101

TypeScript Version: 2.5.3
(Tested at https://www.typescriptlang.org/play/)

Looks like Typescript is not correctly evaluating the generic return type where all the fields are optional.

Here is the minimal case.
Code

interface Base { id: string }
type Spec<T extends Base> = { [K in keyof T]?: T[K] }
export const makeSpec = <T extends Base>(id: string): Spec<T> => {
    return { id }
}

Expected behavior:
Should compile

Actual behavior:
It throws an error for second last line:
Type '{ id: string; }' is not assignable to type 'Spec<T>'.

The compiler doesn't error on this:

interface A extends Base { name: string }
const spec: Spec<A> = { id: 'test' }

I feel it is a bug but I may be misunderstanding something.

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