Skip to content

Extract<string, 'a'> != 'a' #47201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
javier-cornejo opened this issue Dec 20, 2021 · 5 comments
Closed

Extract<string, 'a'> != 'a' #47201

javier-cornejo opened this issue Dec 20, 2021 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@javier-cornejo
Copy link

Bug Report

Extract<string, 'a'> is never, while it should be 'a'. A type like string could be seen as a virtual union of all literal string types. Therefore when extracting a literal from a more general type it is assignable to, the literal type should be returned.
In other words, Extract should be implemented as:

type Extract<A,B> = A extends B ? A : B extends A ? B : never

🔎 Search Terms

Extract utility type
Extract literal from type

🕗 Version & Regression Information

This is present in v3.3.3 (oldest available in playground)

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about distributive conditionals, negated types
  • I was unable to test this on prior versions because they're not available in playground

⏯ Playground Link

Playground link with relevant code

💻 Code

// Cannot extract a narrow literal from its general type
type T01 = Extract<string, 'a'> // never
const a1: T01 = 'a'

// Alternative implementation
type AltExtract<A,B> = A extends B ? A : B extends A ? B : never
type T02 = AltExtract<string, 'a'> // a
const a2: T02 = 'a'

// Still matches old definitions
type T03 = AltExtract<"a" | "b" | "c", "a" | "f">
type T04 = AltExtract<string | number | (() => void), Function>;

🙁 Actual behavior

Extract<string, 'a'> is never

🙂 Expected behavior

Extract<string, 'a'> is 'a'

@MartinJohns
Copy link
Contributor

Exclude<> operates on union types. string is not a union type, so there's nothing to exclude from. TypeScript does not have the ability to represent the a type "string, except this specific string". This would require #29317.

This is a duplicate from the 2 days old #47178, and many more.

@javier-cornejo
Copy link
Author

@MartinJohns I think it's analogous to the issue you mention it duplicates, but it does not require negated types. I've suggested an alternative implementation of Extract that works without changes to the language.

@MartinJohns
Copy link
Contributor

I completely misread Extract as Exclude. However, the comment about union remains true.

This is not a bug, but working as intended. You might want to rephrase it as a feature request.

@VanCoding
Copy link

I think what you describe is not a bug, but the intended way that Extract works.
What you want could be done with custom types like this, though:

type Narrow<Base,Target> = Target extends Base?Target:never

type A = Narrow<string,"a">

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 20, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants