-
-
Notifications
You must be signed in to change notification settings - Fork 6
👍 Add isParametersOf
#74
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
Conversation
|
It seems the function is quite resemble with import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
const isMyType = is.TupleOf(
[is.Number, is.String, is.Boolean],
is.ArrayOf(is.Number),
);
const a: unknown = [0, "a", true, 0, 1, 2];
if (isMyType(a)) {
// a is narrowed to [number, string, boolean, ...number[]]
const _: [number, string, boolean, ...number[]] = a;
}What is the difference and advantage compared to the existing one? |
|
How can I use It is possible by defining a custom Predicate import { is } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
function f(name: string, optCount?: number, optFlag?: boolean): void {}
const arglist: unknown[] = [
["a", 1, true],
["a", 1],
["a"],
["a", true, 1], // Expect skip
["a", 1, true, true, true], // Expect skip
];
for (const args of arglist) {
if (is.TupleOf([is.String] as const, is.ArrayOf(is.UnionOf([is.Number, is.Boolean])))(args)) {
// args is narrowed to [string, ...(number, boolean)[]]
f(...args); // deno-ts:Error:2345:Argument of type 'number | boolean' is not assignable to parameter of type 'number | undefined'. Type 'boolean' is not assignable to type 'number'.
}
if (is.UnionOf([is.TupleOf([is.String]), is.TupleOf([is.String, is.Number]), is.TupleOf([is.String, is.Number, is.Boolean])])(args)) {
// args is narrowed to [string] | [string, number] | [string, number, boolean]
f(...args); // deno-ts:Error:2556:A spread argument must either have a tuple type or be passed to a rest parameter.
}
const isOpts = (_x: unknown): _x is [number?, boolean?] => /* nice implements */ true;
if (is.TupleOf([is.String], isOpts)(args)) {
// args is narrowed to [string, (number | undefined)?, (boolean | undefined)?]
f(...args); // No error
}
if (is.ParametersOf([is.String, is.OptionalOf(is.Number), is.OptionalOf(is.Boolean)] as const)(args)) {
// args is narrowed to [string, (number | undefined)?, (boolean | undefined)?]
f(...args); // No error
}
} |
|
Ah got it. |
lambdalisue
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to make the description a bit more clear but LGTM.
lambdalisue
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
Motivation
I want to validate method arguments with optional parameters.
Comparison with
isTupleOf()isTupleOf(): the length of the tuple is fixed.isParametersOf(): the tuple is truncated length if trailing predicates are optional.Naming
If there is a better name, please suggest.