Skip to content

constraint causes too specific generic type inference #61989

Closed as not planned
Closed as not planned
@marcospgp

Description

@marcospgp

🔎 Search Terms

I'm writing a generic function:

export function useKeyValueStore<T extends string | number | boolean>(
  key: string,
  initialValue: T,
): [T, (value: T) => void] {

if I call it with useKeyValueStore("someKey", 100), T gets inferred as 100

if I remove the extends string | number | boolean type constraint, it gets inferred as number

this seems like unintended behavior - I'd like to keep the constraint and infer T as number

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

// Your code here

🙁 Actual behavior

see above

🙂 Expected behavior

see above

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions