Home > Enterprise >  How can I remove require from TS output file for properties defined with declare?
How can I remove require from TS output file for properties defined with declare?

Time:06-02

I have the file with declarations for the external JS library: events.ts

export declare EVENT_PLAYER_COMMAND_TEXT: string
... more other events

I import this event in my index.js:

import { EVENT_PLAYER_COMMAND_TEXT } from '../types/default/events';
on(EVENT_PLAYER_COMMAND_TEXT, () => {})

In output JS file I get:

var event_1 = require('../types/default/events')
on(event_1.EVENT_PLAYER_COMMAND_TEXT, () => {})

BUT, the library expects that I will use EVENT_PLAYER_COMMAND_TEXT without event_1 or any other variable. So the output result in JS must be:

on(EVENT_PLAYER_COMMAND_TEXT, () => {})

How could I achieve that?

Thanks.

CodePudding user response:

You can preserve import/export statements with the module setting in tsconfig.

Any value that is es20XX or esnext should preserve import export statements.

But this setting is for making sure that the import/export syntax works in your target environment. So depending on where you run this code, this change may break imports entirely.

That said, if import getting transpiled to require works in your target environment at all, then the way typescript transpiled that should work just fine. It should functionally identical.

  • Related