@homer0/ts-async-import
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

☢️ TS Async Import

A wrapper to do dynamic imports in TypeScript contexts that target CommonJS.

🍿 Usage

⚠️ Don't use this package if you are targeting ES modules.

⚙️ Examples

import { tsAsyncImport } from '@homer0/ts-async-import';

// ...

const { default: nodeFetch } =
  await tsAsyncImport<typeof import('node-fetch')>('node-fetch');

That's it, the only difference between this and a regular import is that you need to specify the type of the module you are importing.

And, if for some reason, you want to specify the context path for the import, you can do it with the second parameter:

import { tsAsyncImport } from '@homer0/ts-async-import';

// ...

const { default: nodeFetch } = await tsAsyncImport<typeof import('node-fetch')>(
  'node-fetch',
  __dirname,
);

🤘 Development

As this project is part of the packages monorepo, some of the tooling, like lint-staged and husky, are installed on the root's package.json.

Tasks

Task Description
lint Lints the package.
test Runs the unit tests.
types:check Validates the TypeScript types.

Motivation

I didn't want to have to change my tsconfig.json just to prevent TS to transform the two imports I have into requires 🤬.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.51latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.51
1.1.40
1.1.30
1.1.20
1.1.10
1.1.00

Package Sidebar

Install

npm i @homer0/ts-async-import

Weekly Downloads

1

Version

1.1.5

License

MIT

Unpacked Size

4.03 kB

Total Files

5

Last publish

Collaborators

  • homer0