Skip to content

Narrow generic mapped argument #57691

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

Closed
6 tasks done
KnightSlayer opened this issue Mar 8, 2024 · 2 comments
Closed
6 tasks done

Narrow generic mapped argument #57691

KnightSlayer opened this issue Mar 8, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KnightSlayer
Copy link

πŸ” Search Terms

"type narrowing for mapped type", "type narrowing for mapped argument", "type narrowing for generic mapped argument"

βœ… Viability Checklist

⭐ Suggestion

I came across a case where the type could obviously be narrowed down. but for some reason typescript doesn't support this. it was a frustrating experience

πŸ“ƒ Motivating Example

Here is code example

type T = 'a' | "b"
type M = {
  a: 1,
  b: 2
}

function f1(a: 'a') {}
function f2(a: 1) {}

function f<TT extends T>(t: TT, p: M[TT]) {
  switch (t) {
    case "a": {
      f1(t);
      f2(p); // Argument of type '1 | 2' is not assignable to parameter of type '1'.  Type '2' is not assignable to type '1'.
      break
    }
  }
}

πŸ’» Use Cases

  1. I want to write more clear code without workarounds
  2. Current approach isn't intuitive
  3. I'm using type casts as workaround in the meantime
@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 8, 2024

The error is correct. It's perfectly valid to write this call:

f<'a' | 'b'>('a', 2);

You would need #27808 for this.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 8, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
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

4 participants