Allow intersection type guards for multiple parameters
See original GitHub issueSearch Terms
type guard multiple parameters
Suggestion
I’d like to write a type guard that takes two parameters and changes the type of both of them.
Use Cases
My specific use case is to try to make the following pattern (somewhat) more type-safe:
class Foo<TFeature, TOther> {
// If featureCtor is null, TFeature will never be used.
constructor(private readonly featureCtor: { new(): TFeature } | null) { }
isFeature(thing: any): thing is TFeature {
return !!this.featureCtor && thing instanceof this.featureCtor;
}
bar(thing: TFeature|TOther) {
if (this.isFeature(thing)) {
// Type guard should prove that this.featureCtor is not null
new this.featureCtor();
} else {
// Type guard should prove this
const x: TOther = thing;
}
}
}
Examples
isFeature(thing: any, ctor: { new(): TFeature } | null): (thing is TFeature)&(ctor is { new(): TFeature }) {
return !!this.featureCtor && thing instanceof this.featureCtor;
}
It would be even nicer to allow type guards to operate on readonly fields, so I wouldn’t need to pass this.featureCtor
as a parameter.
I also tried
constructor(private readonly featureCtor: TFeature extends never ? null : { new(): TFeature }) { }
But that didn’t work.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript / JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. new expression-level syntax)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:74
- Comments:9
Top Results From Across the Web
Can I write a type guard that asserts multiple invariants?
Of course, it's not necessary to use that operator, the type intersection operator, in this case because we can simplify it to
Read more >Handbook - Unions and Intersection Types - TypeScript
An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has...
Read more >Advanced TypeScript Types Cheat Sheet (with Examples)
An intersection type is a way of combining multiple types into one. This means that you can merge a given type A with...
Read more >How to use type guards in TypeScript - LogRocket Blog
A type guard is a TypeScript technique used to get information about the type of a variable, usually within a conditional block.
Read more >How To Do Anything in TypeScript With Type Guards
Type guards are conditional checks that allow types to be narrowed from general types to more specific ones. With type guards, we do ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seconding a need for this:
Hi, any updates?