Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
In Typescript, can I constrain the type of the value at a generic key?

In Typescript, how can I constrain the type a field on a generic type to a particular type? In other words, I want to write a function that accepts an object obj and a key key of that object, but raises a Typescript error if obj[key] is not of type X. For instance, if X is string:

function foo<T extends {}, K extends keyof T ???>(obj: T, key: K) { 
  console.log(obj[key])
}

>> foo({ fox: 'Mulder' }, 'fox')
Mulder

>> foo({ fox: 22 }, 'fox')
<some error here>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Guerric's answer demonstrates how type parameters are usually constrained: 1) define T constrained to be an object, then 2) define K constrained to be a key of T.

For the OP's case, however, I would reverse this logic: 1) define Key constrained to be a generic property key (a.k.a string | number | symbol), then 2) define Obj constrained to be an object containing property Key:

function foo<Key extends PropertyKey, Obj extends Record<Key, string>>(obj: Obj, key: Key) {
    console.log(obj[key]);
}

foo({ fox: 'Mulder' }, 'fox'); // works fine

foo({ fox: 22 }, 'fox'); // Error (number is not assignable to string)

Try it.

This way, you get readable errors (in this context, "number is not assignable to string" is much less confusing than "string is not assignable to never"), and also the implementation of the function (the console.log(obj[key]) bit) does not require any typecasting since obj is properly typed to have a Key key.

about 4 years ago · Juan Pablo Isaza Report

0

You could do like this:

function foo<T extends {}, K extends keyof T>(obj: T, key: T[K] extends string ? T[K] : never) { 
  console.log((obj as any)[key]);
}

foo({ fox: 'Mulder' }, 'fox'); // OK


foo({ fox: 22 }, 'fox'); // Argument of type 'string' is not assignable to parameter of type 'never'

It implies to cast obj as any in the implementation because never can't be used as an index type, even though nothing is assignable to never so a function call with an actual key parameter that would make key type resolve to never will never compile (by definition), but TypeScript is not clever enough to infer that.

TypeScript playground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!