Skip to content

Can't assign to conditional generic within generic #41837

Closed
@ayroblu

Description

@ayroblu

Hi I was trying to do something much more complicated than this with conditionals without success, I was reducing the issue down and down and I found this, which I find a bit weird, I was hoping someone might be able to enlighten me of a work around?

TypeScript Version: v4.1.2

Search Terms:
assign to conditional generic

Code

type Param<T> = T extends undefined ? {
    first: string,
} : {
    first: string,
}
function run<T>(param: T) {
    const p: Param<T> = {
        first: 'hi'
    }
    return p;
}

Expected behavior:
No type error

Actual behavior:
type error

Playground Link:
link here

Related Issues:
I couldn't find any, sorry if this is a well known problem / duplicate!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions