docs/dev-api/ts-import.md
tsImport()Native dynamic import() function to import TypeScript files (supports top-level await).
Use this function for importing TypeScript files without adding TypeScript support to the entire runtime.
The current file path must be passed in as the second argument to resolve the import context.
Since this is designed for one-time use, it does not cache loaded modules.
import { tsImport } from 'tsx/esm/api'
const loaded = await tsImport('./file.ts', import.meta.url)
// If tsImport is used to load file.ts again,
// it does not yield a cache-hit and re-loads it
const loadedAgain = await tsImport('./file.ts', import.meta.url)
If you'd like to leverage module caching, see the ESM scoped registration section.
const { tsImport } = require('tsx/esm/api')
const loaded = await tsImport('./file.ts', __filename)
tsconfig.jsontsconfig.json pathtsImport('./file.ts', {
parentURL: import.meta.url,
tsconfig: './custom-tsconfig.json'
})
tsconfig.json lookuptsImport('./file.ts', {
parentURL: import.meta.url,
tsconfig: false
})
Detect files that get loaded with the onImport hook:
tsImport('./file.ts', {
parentURL: import.meta.url,
onImport: (file: string) => {
console.log(file)
// file:///foo.ts
}
})