Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Search 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)
Reacted by Theodor Diesner-Mayer, Ruslan Fadeev, Osyris, Hinase, Alexandre Côté, Richard Beddington, Matus Kislan, Alexey Belostotskiy, Kyle Herock, Klaus Reimer and 143 more
Reacted by matheusrsimon
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
jyrodrigues commentedon May 20, 2019
Any news on this?
quisido commentedon Nov 19, 2019
Seconding a need for this:
kayahr commentedon Dec 5, 2019
This would be useful for processing overloaded function parameters where it is enough to check one parameter to assume the type of the other parameter:
Fnxxxxo commentedon Dec 14, 2019
This is also convenient and meaningful for builders.
m5r commentedon Jul 27, 2020
Hi, any updates?
mordv commentedon May 16, 2021
I think it would be useful for otherwise annoying ref.current checking in react
avalanche1 commentedon May 24, 2022
bump!
helmturner commentedon Jul 15, 2022
Would be great for syntax trees, as well.
I actually assumed this was possible, and tried the following syntaxes before finding this issue.
This syntax seems natural:
But it logically follows that a destructuring pattern could be used, as well.
Something like the following:
or:
joyt commentedon Dec 7, 2022
Would be really useful to have this. I wanted something like then when trying to type a
deepEqualfunction:fatso83 commentedon Jan 30, 2023
Currently, the following is not possible:
Even though the predicate implicitly guarantees that A and B are both sets, it has lost this knowledge when applied to the arrays constituents. Instead I have to either repeat calls to type predicates for each variable like this:
Or I need to create throwaway intermediate objects to work around this, bloating the code:
Would be cool to avoid this.
trusktr commentedon Mar 2, 2023
A couple questions for this in the wild:
https://stackoverflow.com/questions/66880821/in-typescript-i-want-to-make-a-type-guard-for-multiple-arguments
https://stackoverflow.com/questions/58636396/type-guard-that-asserts-all-rest-parameters-are-not-undefined
WillsterJohnson commentedon Jun 1, 2023
It's worth noting that without this feature TypeScript is logically inconsistent, see #54479
LiChangyi commentedon Nov 14, 2023
Hi, any updates? I need it.
fatso83 commentedon Nov 14, 2023
If you want it, make it and supply a PR
justingrant commentedon Apr 10, 2024
Another use case for this feature is validating that an object contains a particular key.
Assume I have a Record
objand a stringkey, wherekeymay be a string literal, a string union type, or a string variable. Today, I'd need two different type guards with the same runtime implementation to validate thatobjhas an own property ofkey:keytokeyof objso I can safely writeobj[key]objto a Record type with a property whose name is typetypeof key, so that I can safely passobjto other functions that require the narrowed type.Ideally I could have a single type guard function that narrows both
objandkeyat the same time.bgenia commentedon Apr 10, 2024
If you do this with a union or a wide type like
stringyou'd essentially prove the existence of multiple keys by checking for only one. Isn't that unsafe?helmturner commentedon Apr 10, 2024
That depends entirely on the runtime implementation. By declaring functions as type guards or assertions, we are already being somewhat less safe by saying "Trust me, Typescript. I know what type this function returns."
polurax commentedon May 30, 2024
I don't know if this issue makes the same request, but it would be great if the type guard also cast the others variables with the same generic type
an exemple
burtek commentedon May 30, 2024
Valentin Turpault (@polurax) the issue is, just because
petisCatdoesn't meanTisCatas well. Example where code compiles butTis notCateven thoughpetisCat:polurax commentedon May 30, 2024
I understand the problem. But it remains disconcerting.
Thank for the explanation
polurax commentedon May 30, 2024
Bartosz (@burtek)
I know it's not possible because of transpilation in javascript, but having a type guard that works on generic types directly without using a variable would be great.
Lonli-Lokli commentedon Sep 16, 2024
With a brilliant fix #57465
Is it possible to reconsider if it possible to implement this one too?
sdwvit commentedon Oct 2, 2025
Much needed feature. In the meanwhile I use a workaround: