Skip to content
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

Generic function type gets inferred to any without explicit typing #51831

Closed
KSchala opened this issue Dec 9, 2022 · 2 comments
Closed

Generic function type gets inferred to any without explicit typing #51831

KSchala opened this issue Dec 9, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KSchala
Copy link

KSchala commented Dec 9, 2022

Bug Report

Type has to be explicit defined while being resolved correctly?!

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

πŸ”Ž Search Terms

Type inference, Union, Pattern

⏯ Playground Link

Playground link with relevant code

πŸ’» 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);
}

πŸ™ Actual behavior

grafik

Typescript throws an type error

πŸ™‚ Expected behavior

Type should be correctly inferred without explicitly defining the type

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 9, 2022
@RyanCavanaugh
Copy link
Member

See #47599

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants