-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Probabilistic Types #34714
Comments
I imagine such a change would break existing TypeScript code; if a union of a literal type with its widened supertype doesn't collapse to the supertype, anything that operates on union members (such as distributive conditional types) could behave differently. With current TS one might achieve something like what you're looking for with a helper function: const MetaSyntacticVar = <T extends string, U extends "foo" | "bar" | "baz">(t: T | U) => t as T;
const intelliSense = MetaSyntacticVar("qux"); // <-- intellisense prompts "foo", "bar", "baz" too |
Thanks for the reply, I changed the title from "Probabilistic Union Types" to just "Probabilistic Types".
What I'm proposing instead is that a type It's a wild idea perhaps, but was prompted by a real problem I was having which I described (in simplified form) in the use cases. |
Seems related to #26277 |
I don't remember in which issue this was discussed, but it was proposed this can be solved with branded tag types. Edit: found it #33471 |
@AlCalzone @orta - Yep, that issue addresses the exact problem I was having. Thinking out loud, if you wanted to exactly specify the expected internal distribution of a type, you could add new syntax. Maybe something like: type MetaSyntacticVar = 'foo'~4 | 'bar'~3 | 'baz'~2 | string~1;
// expect string 10% of the time, etc Intellisense could use it for sorting, or linters could warn for values falling below certain thresholds. An IDE could even give feedback about how likely some logic branch would ever be executed, which would be useful during refactoring. |
Search Terms
Probabilistic, union, supertypes
Suggestion
If a union type contains a member which is a supertype of one or more other members, the supertype subsumes those other members, but the information isn't lost. Tooling can then use that information as a hint about the expected distribution of possible values within that type.
For the purposes of type safety, the above type is simply
string
. However, tools may consider'foo' | 'bar' | 'baz'
to be the most likely values forMetaSyntacticVar
. An IDE might use them for autocomplete. A linter (i.e. not tsc) might flag other values as a warning. Etc.Use Cases
A backend service offers a range of options, and I want to statically model them as a union type. Sometimes, the service adds a new option. I must either generalize the type to a string, do a type cast and tolerate some type unsafety, or release code updates in sync with all backend updates.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: