Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@anurbol

Description

@anurbol
Issue body actions

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.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.