Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i import a single type, class, const or enum from a sub namespace inside from a Typescript module?

Tags:

typescript

I am working with Google Cloud Platform and Typescript.

I've working with some code and varialbes that should implement specific GPC types. like:

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

I would like to import only types, classes and enums I would like to use, for instance

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

How can i import a single type, class or enum from a sub namespace inside from a module?

like image 654
Daniel Santos Avatar asked Sep 01 '25 03:09

Daniel Santos


1 Answers

In short, you can't do that.

Summarizing from the link: even though the syntax looks similar, importing named imports is not the same thing as destructuring an object so you can't nest it like you can with destructuring. You will have to use two statements:

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

like image 123
Jared Smith Avatar answered Sep 02 '25 18:09

Jared Smith