Skip to content

tsc needlessly emits Object.defineproperty() line when "import" keyword is used. #47563

Closed
@KumanekoSakura

Description

@KumanekoSakura

Bug Report

I want to share interface definitions between "a .ts file for server side" (say, server.ts) and "a .ts file for client side" (say, client.ts) using a shared .ts file (say, interfaces.ts) in order to be able to catch interface definition mismatch errors. I want to use interfaces.ts as if a .h file in C language.

server.js (which runs using Node.js + Express) emits a HTML response which contains

  <script type='text/javascript' src='client.js'></script>

in order to let web browser parse client.js file.

But tsc command needlessly emits

  Object.defineproperty(exports, "__esModule", { value: true });

line into client.js (which causes

  Uncaught ReferenceError: exports is not defined

error when parsed by a web browser) if I use "import" keyword from client.ts file.

As a result, currently I have to embed output of

  sed -e 's/export //' interfaces.ts

into client.ts file.

Maybe this error could be avoided by specifying non-default values in my tsconfig.json (generated by "tsc --init"), but I am not familiar with JavaScript/TypeScript specifications.

🕗 Version & Regression Information

tsc 4.5.4

💻 Code

In C language, I can do like below.

------ include.h start ------
struct struct1 {
  int key;
  char *value;
};
------ include.h end ------

------ server.c start ------
#include "include.h"

// Do something using "struct struct1" here.
------ server.c end ------

------ client.c start ------
#include "include.h"

// Do something using "struct struct1" here.
------ client.c end ------

In TypeScript language, I tried like below.

------ interfaces.ts start ------
"use strict";
export interface struct1 {
  key: number;
  value: string;
}
------ interfaces.ts end ------

as include.h and

------ server.ts start ------
"use strict";
import { struct1 } from './interfaces';

// Do something using "interface struct1" here.
------ server.ts end ------

------ client.ts start ------
"use strict";
import { struct1 } from './interfaces';

// Do something using "interface struct1" here.
------ client.ts end ------

🙁 Actual behavior

tsc emits

  Object.defineproperty(exports, "__esModule", { value: true });

line (and causes runtime error on the browser side) when "import" keyword is used.

🙂 Expected behavior

tsc does not emit

  Object.defineproperty(exports, "__esModule", { value: true });

line. (Or, tsc makes sure that

  Uncaught ReferenceError: exports is not defined

error does not happen.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions