Kub*_*oda 53 typing typescript definitelytyped
假设库X的打包文件包含一些接口.
interface I1 {
x: any;
}
interface I2 {
y: {
a: I1,
b: I1,
c: I1
}
z: any
}
Run Code Online (Sandbox Code Playgroud)
为了使用这个库,我需要传递一个与其完全相同的对象I2.y.我当然可以在源文件中创建相同的界面:
interface MyInterface {
a: I1,
b: I1,
c: I1
}
let myVar: MyInterface;
Run Code Online (Sandbox Code Playgroud)
但是后来我承担了使用库中的那个保持最新的负担,而且它可能非常大并导致大量代码重复.
因此,有没有办法"提取"接口的这个特定属性的类型?类似的东西let myVar: typeof I2.y(不起作用,导致"找不到名字I2"错误).提前致谢.
编辑:在TS Playground中玩了一下后,我注意到以下代码完全符合我的要求:
declare var x: I2;
let y: typeof x.y;
Run Code Online (Sandbox Code Playgroud)
但是,它需要x声明一个冗余变量.我正在寻找一种方法来实现这个没有这个声明.
Mic*_*zyn 110
以前不可能,但幸运的是现在,因为TypeScript版本2.1.它已于2016年12月7日发布,它引入了索引访问类型,也称为查找类型.
语法看起来与元素访问完全相同,但是代替类型编写.所以在你的情况下:
interface I1 {
x: any;
}
interface I2 {
y: {
a: I1,
b: I1,
c: I1
}
z: any
}
let myVar: I2['y']; // indexed access type
Run Code Online (Sandbox Code Playgroud)
现在myVar有类型的I2.y.
Jam*_*ond 22
只是从联合对象类型中提取文字类型的示例:
type Config = {
key: "start_time",
value: number,
} | {
key: "currency",
value: string,
}
export type ConfigKey = Config["key"];
// "start_time"|"currency"
Run Code Online (Sandbox Code Playgroud)
Gab*_*son 19
keyof Colors将返回所有键的列表"white" | "black"。当此键列表传递到 Colors 接口时,类型将是给定键的所有值"#fff" |\xc2\xa0#000。
interface Colors {\n white: "#fff"\n black: "#000"\n}\n\ntype ColorValues = Colors[keyof Colors]\n// ColorValues = "#fff" | "#000"\nRun Code Online (Sandbox Code Playgroud)\n
要扩展已接受的答案,您还可以使用type关键字分配类型并在其他地方使用它。
// Some obscure library
interface A {
prop: {
name: string;
age: number;
}
}
// Your helper type
type A_Prop = A['prop']
// Usage
const myThing: A_prop = { name: 'June', age: 29 };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15401 次 |
| 最近记录: |