Home > Software design >  Is it possible to define a parameter of a type that can not be imported but exists in another import
Is it possible to define a parameter of a type that can not be imported but exists in another import

Time:09-23

When importing the following sample type from an external package

interface Config {
  aField: AField;
}

interface AField {
  subField: SubField;
}

interface SubField {}

export { Config };

I can expect a parameter of type Config like so

import { Config } from "external-package";

function myFn(config: Config) {}

but what if this function only needs the variable subField? It's not possible to import this type but is there a way to change the function to something like

import { Config } from "external-package";

function myFn(subField: Config.AField.SubField) {}

As a sidenote: I don't want to import that type from the node_modules directly. If that's the only way I would stick to the first approach...

CodePudding user response:

You can do the following:

function myFn(subField: Config['aField']['subField']) {}
  • Related