admin管理员组文章数量:1305252
As I understand it, interfaces are only relevant in Typescript and the tsc piler should be smart enough not to convert them to JS files in the final output. When I pile my code the interfaces are being piled with it. Why is this?
My simplified project structure
src/
index.ts
lib/
EventClient.ts
interfaces/
EventClientConfig.ts
What it piles to
dist/
index.js
lib/
EventClient.js
interfaces/
EventClientConfig.js
My tsconfig.json
{
"pilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"lib": ["ES6", "DOM"],
"target": "ES2018",
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": ".",
"removeComments": true,
"strict": true,
"typeRoots": ["./node_modules/@types"],
"rootDir": "./src",
"types": [
"node",
"jest-fetch-mock",
"express",
"reflect-metadata"
]
},
"include": ["src"],
"exclude": ["dist", "node_modules", "**/*spec.ts"]
}
An interface
export interface EventClientConfig {
endpoint: string;
authToken: string;
}
How I'm using the interface
import { EventClientConfig } from '../interfaces/EventClientConfig';
const config: EventClientConfig = {
endpoint: '/api',
authToken: '123456'
}
What the interface is piled to
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
In the piled code EventClient.js makes no actual reference to the EventClientConfig.js so it's definitely not required. Why is Typescript piling the interface files to JS?
As I understand it, interfaces are only relevant in Typescript and the tsc piler should be smart enough not to convert them to JS files in the final output. When I pile my code the interfaces are being piled with it. Why is this?
My simplified project structure
src/
index.ts
lib/
EventClient.ts
interfaces/
EventClientConfig.ts
What it piles to
dist/
index.js
lib/
EventClient.js
interfaces/
EventClientConfig.js
My tsconfig.json
{
"pilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"lib": ["ES6", "DOM"],
"target": "ES2018",
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": ".",
"removeComments": true,
"strict": true,
"typeRoots": ["./node_modules/@types"],
"rootDir": "./src",
"types": [
"node",
"jest-fetch-mock",
"express",
"reflect-metadata"
]
},
"include": ["src"],
"exclude": ["dist", "node_modules", "**/*spec.ts"]
}
An interface
export interface EventClientConfig {
endpoint: string;
authToken: string;
}
How I'm using the interface
import { EventClientConfig } from '../interfaces/EventClientConfig';
const config: EventClientConfig = {
endpoint: '/api',
authToken: '123456'
}
What the interface is piled to
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
In the piled code EventClient.js makes no actual reference to the EventClientConfig.js so it's definitely not required. Why is Typescript piling the interface files to JS?
Share Improve this question asked May 8, 2020 at 14:47 punkrockbuddyhollypunkrockbuddyholly 9,7947 gold badges39 silver badges72 bronze badges 3- 2 If you don't like this behavior you can use a declaration file.d.ts. Also, don't confuse a module with something it exports. – Aluan Haddad Commented May 8, 2020 at 15:01
- 1 @AluanHaddad that solves problem. I renamed all the interface files so that they had a .d.ts suffix and they're no longer in the piled directory. Thanks! – punkrockbuddyholly Commented May 8, 2020 at 15:10
- Glad I could help – Aluan Haddad Commented May 8, 2020 at 15:27
2 Answers
Reset to default 6This was solved thanks to @AluanHaddad who suggested using .d.ts declaration files. I have changed all my interface filenames so that they end with .d.ts and they are no longer included in the output.
The answer to your questions is here modules
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
本文标签: nodejsWhy are my typescript interfaces being compiled to javascriptStack Overflow
版权声明:本文标题:node.js - Why are my typescript interfaces being compiled to javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741798780a2398096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论