-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infer union type based on string literal #51734
Comments
I found this related issue but it imply to know the |
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 |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
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: