gpt4 book ai didi

typescript - 如何从 Typescript 模块内部的子命名空间导入单个类型、类、常量或枚举?

转载 作者:行者123 更新时间:2023-12-04 15:05:21 24 4
gpt4 key购买 nike

我正在使用 Google Cloud Platform 和 Typescript。
我正在处理一些应该实现特定 GPC 类型的代码和变量。喜欢:

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>
{ /* ... */ }
}

我只想导入 类型 , 类(class) 枚举 我想使用,例如
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>
{ /* ... */ }
}
如何从模块内部的子命名空间导入单个类型、类或枚举?

最佳答案

总之,you can't do that .
从链接总结:即使语法看起来相似,导入命名导入与解构对象不同,因此您不能像解构那样嵌套它。您将不得不使用两个语句:

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;
Playground

关于typescript - 如何从 Typescript 模块内部的子命名空间导入单个类型、类、常量或枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66250577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com