所以我希望我可以使用一个看起来像这样的丑陋类型的别名:
Maybe<Promise<Paged<Carrier>, Problem>>[]
Run Code Online (Sandbox Code Playgroud)
就像是:
import Response = Maybe<Promise<Paged<Carrier>, Problem>>[];
Run Code Online (Sandbox Code Playgroud)
有没有办法在TypeScript中键入别名?
Mar*_*ski 118
从版本1.4开始,Typescript支持类型别名(源).
键入别名
您现在可以使用type关键字为类型定义别名:
Run Code Online (Sandbox Code Playgroud)type PrimitiveArray = Array<string|number|boolean>; type MyNumber = number; type NgScope = ng.IScope; type Callback = () => void;类型别名与其原始类型完全相同; 它们只是替代名称.
从版本1.6开始,Typescript支持泛型类型别名(源代码).
通用类型别名
在TypeScript 1.6之前,类型别名仅限于缩短长类型名称的简单别名.不幸的是,如果不能制作这些通用的,它们的用途有限.我们现在允许类型别名是通用的,赋予它们完整的表达能力.
Run Code Online (Sandbox Code Playgroud)type switcharoo<T, U> = (u: U, t:T)=>T; var f: switcharoo<number, string>; f("bob", 4);
TSV*_*TSV 10
TypeScript支持导入,例如:
module A {
export class c {
d: any;
}
}
module B {
import moduleA = A;
var e: moduleA.c = new moduleA.c();
}
module B2 {
import Ac = A.c;
var e: Ac = new Ac();
}
Run Code Online (Sandbox Code Playgroud)
从TS 1.4开始,我们可以使用类型声明:
type MyHandler = (myArgument: string) => void;
var handler: MyHandler;
Run Code Online (Sandbox Code Playgroud)
从TS 1.6开始,我们可以使用本地类型声明:
function f() {
if (true) {
interface T { x: number }
let v: T;
v.x = 5;
}
else {
interface T { x: string }
let v: T;
v.x = "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
穷人的解决方案是声明一个t具有所需类型的虚拟变量(例如)并使用typeof t而不是long类型表达式:
var t: { (x: number, f: { (foo: string, bar:boolean): void }): void };
var f: typeof t;
var g: typeof t;
| 归档时间: |
|
| 查看次数: |
39370 次 |
| 最近记录: |