Zev*_*itz 2 typescript conditional-types
我有一个带有三个参数的类方法声明:
declare class Application {
PrintOut(OutputFileName?: string, PrintToFile?: boolean, StartPage?: number): void;
}
Run Code Online (Sandbox Code Playgroud)
打印到文件时,必须传递输出文件名;不打印到文件时,不能传递文件名。这意味着OutputFileName参数的类型取决于PrintToFile参数,如下所示:
PrintToFile是true,则OutputFileName是类型string而不是undefined——即不是可选的。PrintToFile is false / undefined, or not passed), then OutputFileName must be undefinedOr, using overloads:
declare class Application {
PrintOut(OutputFileName: string, PrintToFile: true, StartPage?: number): void;
PrintOut(OutputFileName?: undefined, PrintToFile?: false, StartPage?: number): void;
}
Run Code Online (Sandbox Code Playgroud)
the following calls are valid:
x.PrintOut('abcd', true);
x.PrintOut(undefined, false);
x.PrintOut(undefined, undefined);
x.PrintOut();
Run Code Online (Sandbox Code Playgroud)
and the following calls are invalid:
x.PrintOut(undefined, true); // first parameter must be a string
x.PrintOut('abcd', false); // cannot pass a filename if not printing to a file
x.PrintOut('abcd', undefined); // (same as above)
x.PrintOut('abcd'); // (same as above)
Run Code Online (Sandbox Code Playgroud)
If there were only the two parameters, I would stick with overloads. But this is just an MCVE. The actual method has 19 parameters, with multiple parameters depending on specific values of each of 3 different parameters; and similar methods are applied to 5 different object types.
Using this:
type IfPrintToFile<T, U> =
T extends true ? U :
undefined;
declare class Application {
PrintOut<T extends boolean>(OutputFileName?: IfPrintToFile<T, string>, PrintToFile?: T, StartPage?: number): void;
}
Run Code Online (Sandbox Code Playgroud)
doesn't prevent these:
x.PrintOut(undefined, true);
x.PrintOut('abcd', undefined);
x.PrintOut('abcd');
Run Code Online (Sandbox Code Playgroud)
while defining the constraint on the method as T extends boolean | undefined still doesn't block these:
x.PrintOut(undefined, true);
x.PrintOut('abcd');
Run Code Online (Sandbox Code Playgroud)
Defining the parameters as required:
type IfPrintToFile<T, U> =
T extends true ? U :
undefined;
declare class Application {
PrintOut<T extends boolean | undefined>(OutputFileName: IfPrintToFile<T, string>, PrintToFile: T, StartPage?: number): void;
}
Run Code Online (Sandbox Code Playgroud)
阻止所有无效调用,但也会阻止以下有效调用:
x.PrintOut();
Run Code Online (Sandbox Code Playgroud)
游乐场1
1、Playground URL中似乎没有保存Options设置,每次都需要手动设置。
现在您可以使用 rest 参数来执行此操作(自 jcalz 在 2018 年的回答以来,情况似乎有所改善!):
declare class Application {
PrintOut<T extends boolean | undefined>(
...args: T extends true ? [
OutputFileName: string,
PrintToFile: T,
StartPage?: number,
] : [
OutputFileName?: undefined,
PrintToFile?: T,
StartPage?: number,
]
): void;
}
const x = new Application();
// valid calls
x.PrintOut('abcd', true);
x.PrintOut(undefined, false);
x.PrintOut(undefined, undefined);
x.PrintOut();
// invalid calls
x.PrintOut(undefined, true);
x.PrintOut('abcd', false);
x.PrintOut('abcd', undefined);
x.PrintOut('abcd');
Run Code Online (Sandbox Code Playgroud)
这会产生完全预期的错误并允许所有有效形式。见操场。
| 归档时间: |
|
| 查看次数: |
1370 次 |
| 最近记录: |