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

Add KeyOf and ValueOf definitions to built-in lib #31438

Closed
SalathielGenese opened this issue May 17, 2019 · 5 comments
Closed

Add KeyOf and ValueOf definitions to built-in lib #31438

SalathielGenese opened this issue May 17, 2019 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@SalathielGenese
Copy link

Search Terms

None -
Empirical experience -

Suggestion

I want to see added KeyOf and ValueOf

Use Cases

Let them distribute union

let a_or_b_002!: KeyOf<{ a: 'Aa' } | { b: 'Bb' }>;      // "a" | "b"
let a_or_b_003!: KeyOf<({ a: 'Aa' } | { b: 'Bb' })>;    // "a" | "b"

Examples

let a_or_b_002!: KeyOf<{ a: 'Aa' } | { b: 'Bb' }>;      // "a" | "b"
let a_or_b_003!: KeyOf<({ a: 'Aa' } | { b: 'Bb' })>;    // "a" | "b"

instead of

let a_or_b_001!: keyof ({ a: 'Aa' } | { b: 'Bb' });     // "a" & "b"

Checklist

My suggestion meets these guidelines:

  • [x ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ x] This wouldn't change the runtime behavior of existing JavaScript code
  • [ x] This could be implemented without emitting different JS based on the types of the expressions
  • [ x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x ] This feature would agree with the rest of TypeScript's Design Goals.
@SalathielGenese
Copy link
Author

Here's implementation

declare type KeyOf<T> = T extends any ? keyof T : never;

declare type ValueOf<T> = T extends any ? T[ keyof T ] : never;

@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 17, 2019
@RyanCavanaugh RyanCavanaugh changed the title Complete keyof which is not distributive Add KeyOf and ValueOf definitions to built-in lib May 17, 2019
@carpben
Copy link

carpben commented May 20, 2019

I use $Values by "utility-types" quite often, and find it useful. Would be nice to see a build-in Typescript version.

@kisaragi-hiu
Copy link

kisaragi-hiu commented Apr 28, 2024

A quick search on GitHub shows a mountain of projects defining their own ValueOf or importing type-fest's ValueOf.

  • ValueOf should exist because keyof already exists, it is implementable and has been implemented as a one liner.
  • KeyOf should exist if ValueOf exists for syntax symmetry. (I'd argue keyof should've never been an operator since it's syntax on top of JS that could've just been a utility type, but that ship has already sailed.)
  • A quick search for "typescript valueof" shows significant interest, including a sort of duplicate issue (valueof Type Operator like keyof #53521)
  • valueof Type Operator like keyof #53521 is a feature request asking for a valueof operator. It was for some reason marked as a Question then closed as if it's just a resolved question for how to implement ValueOf yourself.

@kisaragi-hiu
Copy link

This is something that would likely be quite welcomed by the TypeScript community.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 29, 2024
@RyanCavanaugh
Copy link
Member

New utility types are declined by policy. See https://github.com/microsoft/TypeScript/wiki/FAQ#new-utility-types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants