Home > front end >  Can I declare nested group of type in which can access by dot-notation in TypeScript?
Can I declare nested group of type in which can access by dot-notation in TypeScript?

Time:11-02

Can I declare nested group of type in which can access by dot-notation in TypeScript?

What I want to do is like the code below:

import { FuncA, FuncB } from './types' // <-- how should I write code in `types.ts` to access types like `FuncA.Param1` ?

export default class MyFunc {
  funcA(param1: FuncA.Param1, param2: FuncA.Param2): FuncA.Response {
    return blabla
  }

  async funcB(param1: FuncB.Param): Promise<FuncB.Response> {
    return blabla
  }
}

export {
  FuncA,
  FuncB,
}

CodePudding user response:

You can do that with namespaces:

export namespace FuncA {
  export type Param1 = string | number
  export type Param2 = boolean
  export type Response = boolean
}

export namespace FuncB {
  export type Param = number
  export type Response = boolean
}
  • Related