有没有办法"extract"TypeScript接口的类型属性?
Is there a way to "extract" the type of TypeScript interface property?
假设库 X 有一个类型文件,其中包含一些接口。
interface I1 {
x: any;
}
interface I2 {
y: {
a: I1,
b: I1,
c: I1
}
z: any
}
为了使用这个库,我需要传递一个与 I2.y
类型完全相同的对象。我当然可以在我的源文件中创建相同的界面:
interface MyInterface {
a: I1,
b: I1,
c: I1
}
let myVar: MyInterface;
但后来我承担了使它与库中的那个保持同步的负担,而且它可能非常大并导致大量代码重复。
因此,有什么方法可以“提取”这个特定属性接口的类型吗?类似于 let myVar: typeof I2.y
的东西(不起作用并导致“无法找到名称 I2”错误)。
编辑:在 TS Playground 玩了一会儿之后,我注意到下面的代码完全符合我的要求:
declare var x: I2;
let y: typeof x.y;
但是它需要声明一个冗余变量x
。我正在寻找一种无需声明即可实现此目的的方法。
接口就像对象的定义。然后 y 是你的 I2 对象的 属性 ,它是某种类型的,在这种情况下 "anonymous".
您可以使用另一个接口来定义 y,然后像这样将其用作您的 y 类型
interface ytype {
a: I1;
b: I1;
c: I1;
}
interface I2 {
y: ytype;
z: any;
}
您可以将您的界面放在一个文件中并使用提取,这样您就可以将它导入到您项目的其他文件中
export interface ytype {
a: I1;
b: I1;
c: I1;
}
export interface I2 {
y: ytype;
z: any;
}
您可以这样导入:
import {I1, I2, ytype} from 'your_file'
以前不可能,但幸运的是现在可以,因为 TypeScript version 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
现在 myVar
的类型为 I2.y
。
要扩展已接受的答案,您还可以使用 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 };
获取所有值,由于keyof Colors
返回所有键的列表,进而获取Colors
接口的所有值
interface Colors {
white: "#fff"
black: "#000"
}
type ColorValues = Colors[keyof Colors]
// ColorValues = "#fff" | "#000"
只是从联合对象类型中提取文字类型的例子
:
type Config = {
key: "start_time",
value: number,
} | {
key: "currency",
value: string,
}
export type ConfigKey = Config["key"];
// "start_time"|"currency"
假设库 X 有一个类型文件,其中包含一些接口。
interface I1 {
x: any;
}
interface I2 {
y: {
a: I1,
b: I1,
c: I1
}
z: any
}
为了使用这个库,我需要传递一个与 I2.y
类型完全相同的对象。我当然可以在我的源文件中创建相同的界面:
interface MyInterface {
a: I1,
b: I1,
c: I1
}
let myVar: MyInterface;
但后来我承担了使它与库中的那个保持同步的负担,而且它可能非常大并导致大量代码重复。
因此,有什么方法可以“提取”这个特定属性接口的类型吗?类似于 let myVar: typeof I2.y
的东西(不起作用并导致“无法找到名称 I2”错误)。
编辑:在 TS Playground 玩了一会儿之后,我注意到下面的代码完全符合我的要求:
declare var x: I2;
let y: typeof x.y;
但是它需要声明一个冗余变量x
。我正在寻找一种无需声明即可实现此目的的方法。
接口就像对象的定义。然后 y 是你的 I2 对象的 属性 ,它是某种类型的,在这种情况下 "anonymous".
您可以使用另一个接口来定义 y,然后像这样将其用作您的 y 类型
interface ytype {
a: I1;
b: I1;
c: I1;
}
interface I2 {
y: ytype;
z: any;
}
您可以将您的界面放在一个文件中并使用提取,这样您就可以将它导入到您项目的其他文件中
export interface ytype {
a: I1;
b: I1;
c: I1;
}
export interface I2 {
y: ytype;
z: any;
}
您可以这样导入:
import {I1, I2, ytype} from 'your_file'
以前不可能,但幸运的是现在可以,因为 TypeScript version 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
现在 myVar
的类型为 I2.y
。
要扩展已接受的答案,您还可以使用 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 };
获取所有值,由于keyof Colors
返回所有键的列表,进而获取Colors
接口的所有值
interface Colors {
white: "#fff"
black: "#000"
}
type ColorValues = Colors[keyof Colors]
// ColorValues = "#fff" | "#000"
只是从联合对象类型中提取文字类型的例子
:type Config = {
key: "start_time",
value: number,
} | {
key: "currency",
value: string,
}
export type ConfigKey = Config["key"];
// "start_time"|"currency"