Ole
Ole

Reputation: 46950

Does Typescript have static imports?

Suppose Object is something that is importable and we currently use Object.getOwnPropertyNames. Could we do:

 import {getOwnPropertyNames} from 'Object';

Upvotes: 5

Views: 7173

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074365

TypeScript uses the ES2015 concept of modules (more here), but Object isn't a module, so you can't do what you've shown, but you can use destructuring assignment instead:

const { getOwnPropertyNames } = Object;

...which is the same as:

const getOwnPropertyNames = Object.getOwnPropertyNames;

For any method that doesn't rely on a particular this value (and the ones on Object don't), you could use the result on its own:

const obj = {a: 1, b: 2};
const { getOwnPropertyNames } = Object;
console.log(getOwnPropertyNames(obj));

Upvotes: 14

Related Questions