Skip to content
  • Paul Gschwendtner's avatar
    build: enable `esModuleInterop` in TypeScript compilations (#43431) · d15a6927
    Paul Gschwendtner authored
    Enables the `esModuleInterop` for all TypeScript compilations in the
    project. This allows us to emit proper ESM-compatible code. e.g.
    consider the following import:
    
    ```ts
    import * as ts from 'typescript';
    ```
    
    This import currently will break at runtime in NodeJS because the
    `typescript` package is not shipping ESM. It's still a CommonJS module.
    ES modules are able to import from `typescript` though, using an import
    statement as above, but everything in `module.exports` is being exposed
    as the `default` named export. TypeScript at runtime does not have any
    other named exports, so for actual ESM compatibility, all of our imports
    need to be switched to:
    
    ```
    import ts from 'typescript';
    ```
    
    The `esModuleInterop` option allows this to work even though the
    `d.ts` file of TS currently suggests that there are _only_ named exports.
    The TypeScript language service will now suggest the correct import form as
    shown above. It doesn't enforce that unfortunately, but this commit also
    adds a lint rule that enforces certain patterns so that we emit imports
    that are compatible with both ESM and CJS output (CJS still needed here
    since tests run with CJS devmode output still; this is a future project
    to switch that over to ESM!)
    
    PR Close #43431
    d15a6927