By Philip Feldmann


2019-09-20 11:44:26 8 Comments

I have the following function:

    const infer = (...params: string[]): Record<string, string> => {
      const obj: Record<string, string> = {};
      // Put all params as keys with a random value to obj
      // [...]
      return obj;
    }

This function will take n strings and return an object containing exactly those strings as keys, with random values.

So infer("abc", "def") might return {"abc": "1337", "def":"1338"}.

Is there any way to infer the return type to get complete type-safety from this function? The code of the function guarantees that each key will be present in the returned object and that each value will be a string.

1 comments

@ford04 2019-09-20 12:40:00

You could declare it like this:

const infer = <T extends string[]>(...params: T): Record<T[number], string> => {
  const obj: Record<string, string> = {};
  // Put all params as keys with a random value to obj
  // [...]
  return obj;
};

const t = infer("a", "b", "c") // const t: Record<"a" | "b" | "c", string>

Playground

@Philip Feldmann 2019-09-20 13:21:52

Amazing, can you explain what the T[number] part does exactly?

@ford04 2019-09-20 14:55:21

Sure. If you have type MyTuple = ["a", "b", "c"], you can use the lookup/index access operator to access item types by their numeric index value, e.g. MyTuple[0] has type "a". number in MyTuple[number] simply stands for all possible numeric index values, so MyTuple[number] is the union of all possible item types, which are your desired string values.

@etarrowo 2020-03-25 15:34:05

What if I pass the keys in as a single list? How can I get the same output?

@ford04 2020-03-25 15:44:29

@etarrowo You can drop the rest parameter from infer and let T extend string, like here

@etarrowo 2020-03-25 16:17:25

awesome thank you @ford04 this one has been puzzling me for a bit

@etarrowo 2020-03-25 16:51:12

I wonder whats up with the reduce type in my implementation bit . ly/39fXNIf (sorry the tsplayground link is too long)

@ford04 2020-03-25 19:17:48

@etarrowo I do not open those shortened links, sorry. That seems to be a different question, feel free to open a new one, so all can answer.

@etarrowo 2020-03-25 20:37:04

stackoverflow.com/questions/60857002/… here is the link to my new question @ford04

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Typescript doesn't infer value of generic object

3 Answered Questions

[SOLVED] TypeScript String Union to String Array

  • 2017-06-11 05:23:45
  • CodeAndCats
  • 17436 View
  • 56 Score
  • 3 Answer
  • Tags:   typescript

1 Answered Questions

3 Answered Questions

[SOLVED] Typescript: type that is union of object keys can't be use as a key for this object

  • 2019-11-07 07:09:49
  • Zenek Wiaderko
  • 415 View
  • 3 Score
  • 3 Answer
  • Tags:   typescript

1 Answered Questions

[SOLVED] Typing Array of T to Array of ReturnType of T

2 Answered Questions

[SOLVED] TypeScript infer return type from used arguments

2 Answered Questions

2 Answered Questions

[SOLVED] Typescript: Infer lookup type for function parameter within object

  • 2018-08-15 10:51:03
  • robjtede
  • 354 View
  • 0 Score
  • 2 Answer
  • Tags:   typescript types

2 Answered Questions

[SOLVED] How to infer typed mapValues using lookups in typescript?

Sponsored Content