You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeT1={type: 'title',title: string}|{type: 'checkbox',checkbox: boolean}constproperties: T1[]=[{type: 'title',title: 'Hello world',},{type: 'checkbox',checkbox: false}]properties.map(el=>{// By checking the value of the type key, the inference is doneif(el.type==='title')returnel[el.type]// There is only one possibility in our unionreturnel[el.type]})properties.map(el=>{// Inference is not done and this does not work even if all types in our union matchreturnel[el.type]})
π» Use Cases
I am querying an external API that returns me a list of properties:
There are some specific property types like rich_text where I need a specific normalization process.
And by default I simply want to return the value.
typePropertyTypes='email'|'checkbox'|'rich_text'typePropertyDenormalizedValueMap={'email': string;'checkbox': boolean;'rich_text': {plain_text: string,blocks: any[]}};typePropertyNormalizedValueMap={'email': string;'checkbox': boolean;'rich_text': string;};typePropertyObject<TextendsPropertyTypes>={[keyinTasstring]: {type: key;}&{[propertyinkey]: PropertyDenormalizedValueMap[key];};}[T];interfaceNormalizer<TextendsPropertyTypes=PropertyTypes>{normalize(data: PropertyObject<T>): PropertyNormalizedValueMap[T];}classRawNormalizerimplementsNormalizer<'email'|'checkbox'>{normalize(data: PropertyObject<'email'|'checkbox'>): PropertyNormalizedValueMap['email'|'checkbox']{// By checking the `type` key it infers the Typeif(data.type==='checkbox')returndata[data.type];// The only possible type in our union make this possiblereturndata[data.type]}normalize2(data: PropertyObject<'email'|'checkbox'>): PropertyNormalizedValueMap['email'|'checkbox']{// This is not possible as there is no inference donereturndata[data.type]}}classRichTextNormalizerimplementsNormalizer<'rich_text'>{normalize(data: PropertyObject<'rich_text'>): PropertyNormalizedValueMap['rich_text']{returndata.rich_text.plain_text}}
It's not practical for TypeScript to evaluate code based on iterating through all possible inhabitants of a union type and seeing if the code checks for each inhabitant - this would be quickly very combinatorially explosive.
This pattern can be represented however, see #47109 for details
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Adding a way to infer the correct type in a union using a value of its own key.
π Motivating Example
If I have the following type, it is a simple Union where we always have a
type
key and we find akey
of the value of thetype
.You can see it directly in the playground:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAKgjFAvFA3gKClUkBcUDkwAlsADYT4A0GWJ5eAzsAE5EB2A5mgL5QA+qGtgh58AYwAWEMQGsARgHsAHlRqTp85XkULyAQzY80aMQrZMoYZgsjNiEBnngBtALpIozmukyZho4jIKal9aINEACQhSUgUoAHcFZlIAE1VMbhDBUP8CdVlFFSzMfM0lPAAzPVIGCBpuNFdjKxsIOyIHADoAWz0wAApopAA+bKgAenGoACEQKFL2DiwpKAA3aoBXaAUK5ehhKBkIEEo9qHYKtog2MWgiBigUszrMIl3B0k6DxB+CQPJ8ABKKDMCDADbMNhQaLOaJfcAQJo0SawKSg84PMykObPSwKBgMIhyIikEhzdhQBQQqAbNhEMw0UHgyHQ0iwz7CJrcQHNay2ewMHp9D4jMYogCSbEuoJudwebAUwEeuIMKWW92VDigCqViWYMmhq2u5121VIWARDwpVOYNLpZigvWAkkZYIhUJhcM5PEBQA
π» Use Cases
I am querying an external API that returns me a list of properties:
I want to normalize this data into:
There are some specific property types like
rich_text
where I need a specific normalization process.And by default I simply want to return the value.
Here is a playground to test the above code: https://www.typescriptlang.org/play?#code/C4TwDgpgBACgTge0nUAVcEDOUC8UDkEAtgIYCWANvlAD4EDGAFhPQNYBGCAHtXfnGSYB9YBC7B8AKEmhIsRMlAARCADsEcUhTIAvCABMAaiQoBXCAFkSYXFADekqAWLkqALiiZgA1QHMA3I4MzGycPB6cCBQQJKqBTvyCjCJiEh4OTk5gFOSqKeIeXj6+ADRBTuwUCGyYHrEgANoAukEAvpKtgTIY8kgQKCAAchpaugbGZpbWthnOpJT4hd5kfvHBLBzci1CR0bFricKi4ttFKwEdXbLQ8H0DAPLsAFYswAA8qFCpavrYt4ogdCQTAAPhmQQarAgICgKygnxI2DOfia6XKUGuHihIDWrSgADJ7OiGmAFP1QLDVFBsajegCVOpNCYxkYTOYrGBIdCmrjAq0GqgedIVqI4AAzEj0aDDJnaPRwD5fcQ-P5kgZArC2f7kwEYUFEpyM0Z6AAU+hIwBIHm1D2erw+IIAlNa1aAZcbxmyppzBXzpPQcphsAAlEgAd3dzPlsKI2WIamA2Ejcv6b0I8yoNHwTA2YXwYNmRqjEDNFqtdJ1jxe9He6dc+CzOdCWydLrubpGxdZkw5DTrC0bIU2PCaBsyAHpx1AAEIwpusc4Y5hQAAG1xX1OhsOAlLF-WwwGXGvRZDFUFLloAdNdcDg8Nmh3nHVA4BBgKY4FTzZaGt+SNeMCFdFJ3hZcEFUCgYTABAgzISpoBvOEEA-KBTFUMhwKgUgoSXMhsGg2D4PRV930-KA-1-MsAMgFonHaIIixTAAmC9yxtUAq3tftMwfXMW2dCsBmTFkJnZaw+xcAdeObEcxycEDUEYPDYWwdQdwIzA4OiKBESXfpoGU9Rd301QpXI8CIGIt8Py-MtKKva5aKgdp6IDREQySVBUmE6MyFjaIiATJNOxTBVDmSY4JALBiQrGVi2wBTiazTAQjlSfMBPYoZYr0bsxM5cL8gkUdZicEibPIqjUoi1JL2yXIiraDogA
The text was updated successfully, but these errors were encountered: