Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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';
like image 202
Ole Avatar asked Dec 17 '22 22:12

Ole


1 Answers

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));
like image 191
T.J. Crowder Avatar answered Dec 28 '22 11:12

T.J. Crowder