-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow intersection type guards for multiple parameters #26916
Comments
Any news on this? |
Seconding a need for this:
|
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: function nonsense(a: number, b: number): number;
function nonsense(a: string, b: string): string;
function nonsense(a: number | string, b: number | string): number | string {
const isNumbers = (a: number | string, b: number | string): a is number & b is number
=> typeof a === "number";
if (isNumbers(a, b)) {
return a * 10 + b * 100;
} else {
return a.length + b.length;
}
} |
This is also convenient and meaningful for builders.
|
Hi, any updates? |
I think it would be useful for otherwise annoying ref.current checking in react const refsValued = <T extends unknown>(...refs: { current: T | undefined | null }[]): ...refs is { current: T }[] =>
!refs.find((r) => !r.current); |
bump! |
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: import {convert} from 'unist-util-is'
import type {Paragraph, Root} from 'mdast'
export const isNonNestedParagraph = convert<Paragraph>(
(node, _, parent): node is Paragraph & parent is Root =>
!!(node.type === "paragraph" && parent?.type === "root")
); But it logically follows that a destructuring pattern could be used, as well. Something like the following: import {convert} from 'unist-util-is'
import type {Paragraph, Root} from 'mdast'
export const isNonNestedParagraph = convert<Paragraph>(
(node, _, parent): [node, parent] is [Paragraph, Root] =>
!!(node.type === "paragraph" && parent?.type === "root")
); or: import {convert} from 'unist-util-is'
import type {Paragraph, Root} from 'mdast'
export const isNonNestedParagraph = convert<Paragraph>(
(node, _, parent): {node, parent} is {node: Paragraph, parent: Root} =>
!!(node.type === "paragraph" && parent?.type === "root")
); |
Would be really useful to have this. I wanted something like then when trying to type a // Would like assert that BOTH parameters are the intersection of their respective types
export function deepEqual<A, B>(a: A, b: B): (a is A & B) && (b is A & B) {...} |
Currently, the following is not possible: if(isArrayOfSets([A,B])) { // isArrayOfSets is a type predicate : `array is Set<any>[]`
return isSuperSet( [...A], [...B])
} 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: if(isSet(A) && isSet(B)) {
return isSuperSet( [...A], [...B])
} Or I need to create throwaway intermediate objects to work around this, bloating the code: const arr = [A,B]
if(isSetArray(arr)) {
const [newA, newB] = arr;
return isSuperSet( [...newA], [...newB])
} Would be cool to avoid this. |
It's worth noting that without this feature TypeScript is logically inconsistent, see #54479 |
Hi, any updates? I need it. |
If you want it, make it and supply a PR |
Another use case for this feature is validating that an object contains a particular key. Assume I have a Record
Ideally I could have a single type guard function that narrows both |
If you do this with a union or a wide type like |
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." |
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
|
@polurax the issue is, just because foo<Pet>(cat, dogShop) |
I understand the problem. But it remains disconcerting. |
@burtek |
With a brilliant fix #57465 |
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:
Examples
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
But that didn't work.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: