Skip to content

Type x is not assignable to {conditional return type y} #28917

Closed
@anurbol

Description

@anurbol

Hello, TS team. I bet this is easy one for you. And I 95% sure it's not a bug (then what?). The code is not entirely mine, but still I wonder why it fails to compile and why TS doesn't break down the error as it usually does.

TypeScript Version: 3.3.0-dev.20181207 (3.1.6 gives the same result, as does the current playground)

Search Terms: "Type x is not assignable to {conditional return type y}"

Code

const fn = <D extends string | undefined>(someParam: string, defaultValue?: D) : D extends undefined ? string | undefined : string => {

  const result = '';

  return result;
}

Expected behavior:
To compile or at least to break down the error.

Actual behavior:
Fails and shows a not very useful error "Type '""' is not assignable to type 'D extends undefined ? string : string'."

Playground Link:
the playground

Is it at all OK to use a conditional type as a return type? Am I missing something?

P.S. This problem came from stackoverflow, I've tried to answer.

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