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

Extend type predicate inference to work with certain type variables #59088

Open
6 tasks done
ethanresnick opened this issue Jul 1, 2024 · 3 comments
Open
6 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ethanresnick
Copy link
Contributor

πŸ” Search Terms

type predicate inference generic

βœ… Viability Checklist

⭐ Suggestion

When a function would normally serve as a type predicate for a discriminated union, but the exact cases being selected by the type predicate are dictated by a type variable, it'd be nice if the type predicate could still be inferred.

πŸ“ƒ Motivating Example

type Thing = A | B | C
type A = { kind: 'A', id: string, /* ... */ }
type B = { kind: 'B', id: string, /* ... */ }
type C = { kind: 'C', id: string, /* ... */ }

function filterByKind<T extends Thing['kind']>(things: Thing[], kind: T) {
  return things.filter((it) => it.kind === kind);
}

// The below would ideally be typed as `A[]`, as the function passed to 
// `filter` would have an inferred type predicate of `it is Thing & { kind: T }`
const x = filterByKind([], 'A');

const someKind = Math.random() < .5 ? 'A' : 'B';
const y = filterByKind([], someKind); // (A | B)[]

πŸ’» Use Cases

I think this comes up somewhat rarely, and the workaround is to simply manually write out the generic type predicate, but, at my company, we did find cases like this when we looked at our type predicates as part of upgrading to TS 5.5

@Andarist
Copy link
Contributor

Andarist commented Jul 1, 2024

This all works on existing narrowing capabilities. So, in a sense, you are not asking about a type predicate inference improvement but for a narrowing improvement. No narrowing happens today here:

type Thing = A | B | C;
type A = { kind: "A"; id: string /* ... */ };
type B = { kind: "B"; id: string /* ... */ };
type C = { kind: "C"; id: string /* ... */ };

function doStuffIfKind<T extends Thing["kind"]>(thing: Thing, kind: T) {
  if (thing.kind === kind) {
    thing;
    // ^? (parameter) thing: Thing
  }
}

What serializable type you'd expect to see here?

@ethanresnick
Copy link
Contributor Author

ethanresnick commented Jul 1, 2024

So, in a sense, you are not asking about a type predicate inference improvement but for a narrowing improvement.

Fair enough!

What serializable type you'd expect to see here?

I guess I'd expect to see Thing & { kind: T } (which probably isn't particularly useful in the function body, but does become useful if it makes it into the inferred return type).

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jul 11, 2024
@RyanCavanaugh
Copy link
Member

@ahejlsberg was kicking an idea around a while back where all narrowing would be done via intersection like what you're proposing there, which would make this work more or less automatically. I don't think we have a tracking issue for it yet though -- still some technical obstacles around e.g. how to handle negative narrowings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants