microsoft / microsoft/TypeScript

Conditional type doesn't narrow primitive types

Open
#26,240 7 comments 10 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Jan 8, 2019.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.1.0-dev.201xxxxx

Search Terms:

Code

type OnlyNumber<T extends number> = T

type ToNumber<T extends number | string> =
    T extends string ? undefined : OnlyNumber<T>

Expected behavior:
No errors

Actual behavior:
Error:

index.ts:37:47 - error TS2344: Type 'T' does not satisfy the constraint 'number'.
  Type 'string | number' is not assignable to type 'number'.
    Type 'string' is not assignable to type 'number'.

37     T extends string ? undefined : OnlyNumber<T>

Playground Link: https://www.typescriptlang.org/play/#src=type%20OnlyNumber%3CT%20extends%20number%3E%20%3D%20T%0D%0A%0D%0Atype%20ToNumber%3CT%20extends%20number%20%7C%20string%3E%20%3D%0D%0A%20%20%20%20T%20extends%20string%20%3F%20undefined%20%3A%20OnlyNumber%3CT%3E%0D%0A

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.