Typescript: Allow to use subset of Array values as type

Created on 21 Jul 2019  ·  5Comments  ·  Source: microsoft/TypeScript

Search Terms

array values subset

Suggestion

See use cases.

Use Cases

Allow users to type keys based of an array - but do not force users to put all the keys from the array values, just a subset of the values present in the array.

Examples

Simple example, this does not compile, but I want this to compile:


const Events = <const>[
  'bar',
  'foo',
];

type MyEvents = typeof Events[number];

export interface Disco {
  elmo: {
    [key in MyEvents]: true
  }
}

const x: Disco = {   // compiles
  elmo: {
    bar: true,
    foo: true
  }
};

const y: Disco = {   // does not compile, because key y.elmo.foo is missing
  elmo: {
    bar: true,
  }
};


const z: Disco = {   // does not compile, because key y.elmo.bar is missing
  elmo: {
    foo: true,
  }
};

Barriers

The reason the above does not compile is because my objects do not implement all these keys:

  'foo'
  'bar'

they only implement 1 of them. But I want to tell TS that as long as it's a subset of the above list, that it's fine.

I tried using key in Partial<MyEvents> but that didn't work (even tho it would be nice if it did?)
Partial could work for array values not just objects? idk.

Note this same problem occurs when just using a type instead of an array:

type MyEvents = 'bar' |'foo'

export interface Disco {
  elmo: {
    [key in MyEvents]: true  // still requires elmo to have both foo and bar props
  }
}

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.
Question

Most helpful comment

This is already a feature (which is how Partial is implemented). Use this:

export interface Disco {
  elmo: {
    [key in MyEvents]?: true // notice the ? modifier
  }
}

At the risk of being banished to the chillaxation zone a second time, I'd like to gently point out that Stack Overflow could be a more appropriate place for issues like this where you're not sure if a feature exists or not. Now please excuse me while I cower behind these trees... 🌳😨🌳

All 5 comments

This is already a feature (which is how Partial is implemented). Use this:

export interface Disco {
  elmo: {
    [key in MyEvents]?: true // notice the ? modifier
  }
}

At the risk of being banished to the chillaxation zone a second time, I'd like to gently point out that Stack Overflow could be a more appropriate place for issues like this where you're not sure if a feature exists or not. Now please excuse me while I cower behind these trees... 🌳😨🌳

Does the feature have a name? Very hard to find documentation on it

Maybe "optional property modifier for mapped types"? It's documented here but it doesn't say the words "optional modifier". It does show how Partial is defined, though.

You an map the union MyEvents to an object type using a conditional type:

type MyEvents = 'foo' | 'bar';

type UnionToRecord<T> = T extends keyof any ? Record<T, true> : never;

interface Disco {
    elmo: UnionToRecord<MyEvents>;
}

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  ·  3Comments

blendsdk picture blendsdk  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

wmaurer picture wmaurer  ·  3Comments