1

In typescript one can do:

type Foo = string[]

but sometimes one wants to model more structured "object like" data and then one can do:

type Bar = [[name: string],[age:number],[consent:boolean]]

Map is defined as Map<K, V> in lib.es2015.collection.d.ts and that's simlar to how Foo is defined above. How would one type a Map similar to Bar above?

const map1 = new Map();
map1.set("name",'bob');
map1.set("age",102);
map1.set("consent",true);

 // as map1's type is { name: string, age: number, consent, boolean } - the compliler
 // should be able to determine the type is `string`
const shouldBeString = map1.get("name");

code

TrevTheDev
  • 2,616
  • 2
  • 18
  • 36
  • 4
    IIRC, it's not possible. Use an object instead. – CertainPerformance Sep 17 '22 at 00:41
  • 1
    What does `[[name: string], [age: number], [consent: boolean]]` mean to you? In TypeScript that's a tuple of one-tuples, and the labels `name`, `age`, and `consent`, are unobservable in the type system. It's essentially `[[string],[number],[boolean]]`, a subtype of `Array<[string] | [number] | [boolean]>`, which is... weird. Are you just trying to represent `{name: string; age: number; consent: boolean}`? If so, can you [edit] to say that instead (and presumably `type Foo = {[k: string]: string}`?) If not, can you explain what you're talking about? – jcalz Sep 17 '22 at 00:58
  • And where does `Set` show up in this question other than the title? There's no key-value mapping in `Set`s, so what is the issue you have with it? – jcalz Sep 17 '22 at 00:59
  • @jcalz, thank you, yes that looks like the solution. - so this is a duplicate. – TrevTheDev Sep 17 '22 at 02:01

0 Answers0