Content-Length: 263902 | pFad | http://github.com/microsoft/TypeScript/issues/61989

AB constraint causes too specific generic type inference · Issue #61989 · microsoft/TypeScript · GitHub
Skip to content

constraint causes too specific generic type inference #61989

@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









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: http://github.com/microsoft/TypeScript/issues/61989

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy