423

I want to be able to assign an object property to a value given a key and value as inputs yet still be able to determine the type of the value. It's a bit hard to explain so this code should reveal the problem:

type JWT = { id: string, token: string, expire: Date };
const obj: JWT = { id: 'abc123', token: 'tk01', expire: new Date(2018, 2, 14) };

function print(key: keyof JWT) {
    switch (key) {
        case 'id':
        case 'token':
            console.log(obj[key].toUpperCase());
            break;
        case 'expire':
            console.log(obj[key].toISOString());
            break;
    }
}

function onChange(key: keyof JWT, value: any) {
    switch (key) {
        case 'id':
        case 'token':
            obj[key] = value + ' (assigned)';
            break;
        case 'expire':
            obj[key] = value;
            break;
    }
}

print('id');
print('expire');
onChange('id', 'def456');
onChange('expire', new Date(2018, 3, 14));
print('id');
print('expire');

onChange('expire', 1337); // should fail here at compile time
print('expire'); // actually fails here at run time

I tried changing value: any to value: valueof JWT but that didn't work.

Ideally, onChange('expire', 1337) would fail because 1337 is not a Date type.

How can I change value: any to be the value of the given key?

styfle
  • 22,361
  • 27
  • 86
  • 128
  • 1
    The package type-fest (https://github.com/sindresorhus/type-fest) has the type ValueOf, as well as many other exceedingly useful utility types - I use it all the time, and would highly recommend it. – Geoff Davids Jul 08 '21 at 08:42

11 Answers11

713

UPDATE: Looks like the question title attracts people looking for a union of all possible property value types, analogous to the way keyof gives you the union of all possible property key types. Let's help those people first. You can make a ValueOf analogous to keyof, by using indexed access types with keyof T as the key, like so:

type ValueOf<T> = T[keyof T];

which gives you

type Foo = { a: string, b: number };
type ValueOfFoo = ValueOf<Foo>; // string | number

For the question as stated, you can use individual keys, narrower than keyof T, to extract just the value type you care about:

type sameAsString = Foo['a']; // look up a in Foo
type sameAsNumber = Foo['b']; // look up b in Foo

In order to make sure that the key/value pair "match up" properly in a function, you should use generics as well as indexed access types, like this:

declare function onChange<K extends keyof JWT>(key: K, value: JWT[K]): void; 
onChange('id', 'def456'); // okay
onChange('expire', new Date(2018, 3, 14)); // okay
onChange('expire', 1337); // error. 1337 not assignable to Date

The idea is that the key parameter allows the compiler to infer the generic K parameter. Then it requires that value matches JWT[K], the indexed access type you need.

jcalz
  • 264,269
  • 27
  • 359
  • 360
  • 3
    Ran into a problem using a string-valued enum with function members. To handle this well, you can use `type StringValueOf = T[keyof T] & string;`. The best docs I found on string enums are the [TypeScript 2.9 release notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html) – karmakaze Aug 19 '19 at 01:46
  • 2
    Another construct I've found useful is `Required[keyof T]`, which represents the values you can get from `t[k]` when `t: T` and `k in t`. – jsalvata Sep 30 '21 at 12:03
  • 2
    @markokraljevic the answer is perfectly valid. You can't assign types as object values as Typescript types (non-primitive types) do not exist at runtime. This solution is for creating types that can be more than one type and unify it all in one instead of copy/pasting long lists of types (string | boolean | MyType). – José Manuel Blasco Feb 03 '22 at 09:56
  • @jcalz can you help here please? https://www.typescriptlang.org/play?#code/MYGwhgzhAECyCeBlALmZBLY0DeAoa0EqGWA5gE4D2ArgA7QC8O+B0AdmALYCmAXABQA3MCGp8i5dG1IBKXhKmlGAPmatWwSmwiUQ3AHQhKpISLEyA3C3XluyauTbRho7lfUBfADTXoYUnymrvLIktJyCtIqaurQmtq6BkYmLubusbb2js5mbr4eLAUFuFLI3OQAZmDA3NAA8gBGAFYAClT0eASp4qGK6eAN3CAhYaRWxcjwtLUVTkyNre0A2gBEA0MrALpWuPFEOa4QvAttlLRLm4zQS9jdvADkHDwAarn3XtDrw49c3PfeODu9383FernenzAg2+IP+21wuAqlHI-D0yGgnGglAqBzEEBkMQICBQaEw+goNHOnEMUKGfhgs02-EsuAKQA – 3gwebtrain Aug 02 '22 at 02:39
  • Don't forget to add `as const` to the end of the object in question if you are just getting generic string types out of this. – Bret Jan 26 '23 at 22:30
  • https://github.com/joonhocho/tsdef has value of – Konstantin Vahrushev Mar 09 '23 at 14:04
  • `type ValueOfFoo = Foo[keyof Foo]; //string | number` is an alternative some might find more direct and readable. – Dem Pilafian Jun 02 '23 at 02:23
129

There is another way to extract the union type of the object:

  const myObj = {
    a: 1,
    b: 'some_string'
  } as const;

  type Values = typeof myObj[keyof typeof myObj];

Result union type for Values is 1 | "some_string"

It's possible thanks to the const assertions (as const part) introduced in TS 3.4.

Dima
  • 1,455
  • 1
  • 10
  • 6
  • 27
    This `const` thing is very valuable, TypeScript will actually provide the values themselves and remove duplicates; It's very good for dictionaries. – John May 07 '20 at 22:35
  • This helped me. If dealing with an enum as a `type`, this can be written `type MyEnum = { A: 1, B: 'some_string' }; type values = MyEnum[keyof MyEnum];` – MarkMYoung May 05 '22 at 20:51
  • Without `as const` this doesn't work. It only returns the type of the value (not the actual value) DON'T forget it. – Claudiu Oct 26 '22 at 14:51
82

If anyone still looks for implementation of valueof for any purposes, this is a one I came up with:

type valueof<T> = T[keyof T]

Usage:

type actions = {
  a: {
    type: 'Reset'
    data: number
  }
  b: {
    type: 'Apply'
    data: string
  }
}
type actionValues = valueof<actions>

Works as expected :) Returns an Union of all possible types

Chris Kowalski
  • 876
  • 5
  • 4
  • I like this one best as it is clear and explicit in what is being done and `valueof` nicely complements Typescript's existing `keyof` and `typeof` operators. It can even be used with objects rather than types, e.g. `type ObjValues = valueof`, where `obj` is an associative array. – Steve Chambers Jun 06 '23 at 08:17
24

With the function below you can limit the value to be the one for that particular key.

function setAttribute<T extends Object, U extends keyof T>(obj: T, key: U, value: T[U]) {
    obj[key] = value;
}

Example

interface Pet {
     name: string;
     age: number;
}

const dog: Pet = { name: 'firulais', age: 8 };

setAttribute(dog, 'name', 'peluche')     <-- Works
setAttribute(dog, 'name', 100)           <-- Error (number is not string)
setAttribute(dog, 'age', 2)              <-- Works
setAttribute(dog, 'lastname', '')        <-- Error (lastname is not a property)
Jose Gomez
  • 829
  • 5
  • 5
13

Try this:

type ValueOf<T> = T extends any[] ? T[number] : T[keyof T]

It works on an array or a plain object.

// type TEST1 = boolean | 42 | "heyhey"
type TEST1 = ValueOf<{ foo: 42, sort: 'heyhey', bool: boolean }>
// type TEST2 = 1 | 4 | 9 | "zzz..."
type TEST2 = ValueOf<[1, 4, 9, 'zzz...']>
Zheeeng
  • 642
  • 10
  • 21
  • 3
    works only with `ReadonlyArray`: `type ValueOf = T extends ReadonlyArray ? T[number] : T[keyof T];`. See https://github.com/piotrwitek/utility-types#valuestypet source – Bohdan Lyzanets Sep 05 '20 at 16:01
11

You can made a Generic for your self to get the types of values, BUT, please consider the declaration of object should be declared as const, like:

export const APP_ENTITIES = {
  person: 'PERSON',
  page: 'PAGE',
} as const; <--- this `as const` I meant

Then the below generic will work properly:

export type ValueOf<T> = T[keyof T];

Now use it like below:

const entity: ValueOf<typeof APP_ENTITIES> = 'P...'; // ... means typing

   // it refers 'PAGE' and 'PERSON' to you
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
7

Thanks the existing answers which solve the problem perfectly. Just wanted to add up a lib has included this utility type, if you prefer to import this common one.

https://github.com/piotrwitek/utility-types#valuestypet

import { ValuesType } from 'utility-types';

type Props = { name: string; age: number; visible: boolean };
// Expect: string | number | boolean
type PropsValues = ValuesType<Props>;
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • This is the best answer because this answers handles array too. Other answers don't as lots of property on arrays reflect otherwise – bugwheels94 Apr 11 '22 at 00:53
3

You could use help of generics to define T that is a key of JWT and value to be of type JWT[T]

function onChange<T extends keyof JWT>(key: T, value: JWT[T]);

the only problem here is in the implementation that following obj[key] = value + ' (assigned)'; will not work because it will try to assign string to string & Date. The fix here is to change index from key to token so compiler knows that the target variable type is string.

Another way to fix the issue is to use Type Guard

// IF we have such a guard defined
function isId(input: string): input is 'id' {
  if(input === 'id') {
    return true;
  }

  return false;
}

// THEN we could do an assignment in "if" block
// instead of switch and compiler knows obj[key] 
// expects string value
if(isId(key)) {
  obj[key] = value + ' (assigned)';
}
Buksy
  • 11,571
  • 9
  • 62
  • 69
1

with type-fest lib, you can do that with ValueOf like that:

import type { ValueOf } from 'type-fest';

export const PATH_NAMES = {
  home: '/',
  users: '/users',
  login: '/login',
  signup: '/signup',
};

interface IMenu {
  id: ValueOf<typeof PATH_NAMES>;
  label: string;
  onClick: () => void;
  icon: ReactNode;
}

  const menus: IMenu[] = [
    {
      id: PATH_NAMES.home,
      label: t('common:home'),
      onClick: () => dispatch(showHome()),
      icon: <GroupIcon />,
    },
    {
      id: PATH_NAMES.users,
      label: t('user:users'),
      onClick: () => dispatch(showUsers()),
      icon: <GroupIcon />,
    },
  ];
Tiavina MIchael
  • 111
  • 2
  • 5
  • In this case `ValueOf` will only return the type of the value instead of the litterral value, so the type of `id` is merely a string rather then the literal union of `'/' | '/users' | '/login' | '/signup'` – vilsbole Apr 22 '23 at 14:09
0

I realize this is slightly off topic, That said every time I've looked for a solution to this. I get sent to this post. So for those of you looking for String Literal Type generator, here you go.

This will create a string Literal list from an object type.

export type StringLiteralList<T, K extends keyof T> = T[keyof Pick<T, K>];

type DogNameType = { name: "Bob", breed: "Boxer" } | { name: "Pepper", breed: "Spaniel" } | { name: "Polly", breed: "Spaniel" };

export type DogNames = StringLiteralList<DogNameType, "name">;

// type DogNames = "Bob" | "Pepper" | "Polly";
Centerwork
  • 139
  • 2
  • 4
-2

One-liner:

type ValueTypesOfPropFromMyCoolType = MyCoolType[keyof MyCoolType];

Example on a generic method:

declare function doStuff<V extends MyCoolType[keyof MyCoolType]>(propertyName: keyof MyCoolType, value: V) => void;
José Cabo
  • 6,149
  • 3
  • 28
  • 39