We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Type has to be explicit defined while being resolved correctly?!
Type inference, Union, Pattern
Playground link with relevant code
type Union<T extends object> = { [P in keyof T]: ({ [Q in "kind"]: P } & T[P]) extends infer U ? { [Q in keyof U]: U[Q] } : never }[keyof T] type UnionMap<U extends { kind: string }> = { [K in U["kind"]]: U extends { kind: K } ? U : never } type ExhaustivePattern<T extends { kind: string }, R> = { [K in T["kind"]]: (union: UnionMap<T>[K]) => R }; type NonExhaustivePattern<T extends { kind: string }, R> = { [K in T["kind"]]?: (union: UnionMap<T>[K]) => R } & {_: (union: T) => R}; type Pattern<T extends { kind: string }, R> = ExhaustivePattern<T, R> | NonExhaustivePattern<T, R>; function match<U extends { kind: string }, T>(union: U, pattern: Pattern<U, T>): T { if((pattern as any)[union.kind]) { return (pattern as any)[union.kind](union as U) as T } return (pattern as any)["_"](union as U) as T } type ValueType = Union<{ String: {value: string}, Number: {value: number}, Boolean: {value: boolean}, Date: {value: Date} }> let value: ValueType = {kind: "String", value: "Hello"}; function main(value: ValueType) { let test1 = match(value, { String: ({value}) => value, Number: ({value}) => value.toString(), _: (token) => "Unknown" }); console.log(test1); let test2 = match<ValueType, string>(value, { String: ({value}) => value, Number: ({value}) => value.toString(), _: ({kind}) => kind }); console.log(test2); }
Typescript throws an type error
Type should be correctly inferred without explicitly defining the type
The text was updated successfully, but these errors were encountered:
See #47599
Sorry, something went wrong.
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
Bug Report
Type has to be explicit defined while being resolved correctly?!
π Version & Regression Information
π Search Terms
Type inference, Union, Pattern
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Typescript throws an type error
π Expected behavior
Type should be correctly inferred without explicitly defining the type
The text was updated successfully, but these errors were encountered: