如何从Typescript模块中的子命名空间导入单个类型、类、常量或枚举?
我正在使用Google云平台和Typescript 我使用了一些应该实现特定GPC类型的代码和变量。比如:如何从Typescript模块中的子命名空间导入单个类型、类、常量或枚举?,typescript,Typescript,我正在使用Google云平台和Typescript 我使用了一些应该实现特定GPC类型的代码和变量。比如: import { protos } from "@google-cloud/aiplatform"; class myClass { public async getModelById(id: string): Promise<protos.google.cloud.aiplatform.v1beta1.IModel> { /* ... */ }
import { protos } from "@google-cloud/aiplatform";
class myClass {
public async getModelById(id: string): Promise<protos.google.cloud.aiplatform.v1beta1.IModel>
{ /* ... */ }
public async getTrainingPipelineById(id: string): Promise<protos.google.cloud.aiplatform.v1beta1.ITrainingPipeline>
{ /* ... */ }
}
import{protos}来自“@googlecloud/aiplatform”;
类myClass{
公共异步getModelById(id:string):承诺
{ /* ... */ }
公共异步getTrainingPipelineById(id:string):承诺
{ /* ... */ }
}
例如,我只想导入我想使用的类型、类和枚举
import { IModel, ITrainingPipeline } from "@google-cloud/aiplatform".protos.google.cloud.aiplatform.v1beta1 ;
// I know that the code above does not works, but you got the idea
class myClass {
public async getModelById(id: string): Promise<IModel>
{ /* ... */ }
public async getTrainingPipelineById(id: string): Promise<ITrainingPipeline>
{ /* ... */ }
}
从“@google cloud/aiplatform.protos.google.cloud.aiplatform.v1beta1”导入{IModel,ITrainingPipeline};
//我知道上面的代码不起作用,但你明白了
类myClass{
公共异步getModelById(id:string):承诺
{ /* ... */ }
公共异步getTrainingPipelineById(id:string):承诺
{ /* ... */ }
}
如何从模块中的子命名空间导入单个类型、类或枚举?简而言之
从链接总结:尽管语法看起来很相似,但导入命名导入与分解对象并不是一回事,所以不能像分解一样嵌套对象。您必须使用两个语句:
import { protos } from "@google-cloud/aiplatform";
let {google: {cloud: {aiplatform: {v1beta1: {IModel, ITrainingPipeline}}}}} = protos;
// or, perhaps more readably
let {IModel, ITrainingPipeline} = protos.google.cloud.aiplatform.v1beta1;
您可以使用
import
别名语法:import-IModel=protos.google.cloud.aiplatform.v1beta1.IModel代码>上述符号适用于对象和属性,在类型定义的情况下,它似乎不起作用>属性“ITrainingPipeline”不存在于类型“typeof v1beta1”上。ts(2339)OBS该类型确实存在