Skip to content

Re-exporting type declaration fails with "transpileOnly": true #1353

Closed

Description

@dandv

Expected Behavior

I expect that running the code below will succeed:

lib.ts

export class PersonClass {
  age: number;
}

export type PersonType = {
  name: string;
}

index.ts

export { PersonClass, PersonType } from './lib';

Actual Behavior

Indeed it does, unless tsconfig.json also contains "ts-node": { "transpileOnly": true }, in which case it fails with:

export { PersonClass, PersonType } from './lib';
                      ^^^^^^^^^^
SyntaxError: The requested module './lib' does not provide an export named 'PersonType'

Steps to reproduce the problem

node --loader=ts-node/esm --experimental-specifier-resolution=node index.ts

Minimal reproduction

https://github.com/dandv/ts-node-1353

Specifications

  • ts-node version: 10.0.0
  • node version: 16.2.0 or 15.14.0
  • TypeScript version: 4.3.2
  • tsconfig.json, if you're using one:
{
  "ts-node": {
    "transpileOnly": true
  },
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext"
  }
}
  • Operating system and version: Ubuntu 20

Activity

  1. cspotcode commented on May 28, 2021

    @cspotcode
    Collaborator

    I'm not sure, but you may need to split your export {} from onto 2 lines, one being export {} and the other being export type{}

    I suggest turning on isolatedModules and then seeing if tsc complains. isolatedModules can be a bit confusing, but essentially it tells the typechecker to complain any time you're doing something that might break in transpileOnly mode.

    I expect that isolatedModules will complain, but that you'll be able to fix the problem like this, or something very similar to this:

    export { PersonClass } from './lib';
    export type { PersonType } from './lib';
    
  2. cspotcode commented on May 28, 2021

    @cspotcode
    Collaborator

    TypeScript's handbook explains the purpose of isolatedModules very well.
    https://www.typescriptlang.org/tsconfig#isolatedModules

  3. dandv commented on May 28, 2021

    @dandv
    Author

    Indeed, isolatedModules warns,

    TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.

    Explicitly adding the export type solved the issue. Thanks @cspotcode!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions