Skip to content
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

Strict version of Extract for literal union types #31474

Open
5 tasks done
ChristianIvicevic opened this issue May 20, 2019 · 1 comment
Open
5 tasks done

Strict version of Extract for literal union types #31474

ChristianIvicevic opened this issue May 20, 2019 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ChristianIvicevic
Copy link

Search Terms

extract, pick, union, literal, string

Suggestion

Currently we have Pick<T, K> to select a type-safe subset of a given type T using a union of keys K. Unfortunately this does not work for unions of literals and requires Extract which subjectively isn't strict enough:

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
type Subset = Extract<ShirtSizes, "l" | "m" | "s" | "this-is-allowed-as-well">;

One caveat is that Extract does not support code completion when writing the literal union for the keys and it allows keys not in the first type as well. The former is error-prone while the latter is not causing a different behavior since the illegal keys are ignored but it can't be strictly checked in the long run. Of course I understand there are use cases wherein you'd like to use Extract more liberally so the definition

type Extract<T, U> = T extends U ? T : never;

is reasonable enough. Nevertheless, I'd like to propose an additional strict variant as follows

// short-hand form
type ExtractStrict<T, U extends T> = Extract<T, U>;
// equivalent full form
type ExtractStrict<T, U extends T> = T extends U ? T : never;

Use Cases

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
// this allows for code completion
type Subset = ExtractStrict<ShirtSizes, "l" | "m" | "s">;
// this is causing an error
type Subset2 = ExtractStrict<ShirtSizes, "l" | "m" | "s" | "illegal-literal">;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@ChristianIvicevic ChristianIvicevic changed the title Strict version of Pick for literal union types Strict version of Extract for literal union types May 20, 2019
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels May 28, 2019
@Vages
Copy link

Vages commented Jun 17, 2021

Great work, @ChristianIvicevic 👏

Historically, the Typescript Team has declined proposals for utility types that improve strictness: #30825 As your improvement has seen no attention for two years, would you consider submitting it to the type-fest package instead? I've opened an issue for it here: sindresorhus/type-fest#222 I think your good work will reach a lot more people there. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants