https://github.com/oresoftware/ts_bug_repro
Reproducing a tsc compilation bug/feature for the TypeScript people.
https://github.com/oresoftware/ts_bug_repro
Last synced: about 1 year ago
JSON representation
Reproducing a tsc compilation bug/feature for the TypeScript people.
- Host: GitHub
- URL: https://github.com/oresoftware/ts_bug_repro
- Owner: ORESoftware
- Created: 2018-05-31T21:44:58.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T22:12:17.000Z (about 8 years ago)
- Last Synced: 2023-03-02T00:26:09.211Z (over 3 years ago)
- Language: Shell
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Project showing a potential bug in tsc transpilation
In package_a, we have a tsconfig.json file with these typeRoots:
```
"typeRoots": [
"./node_modules/@types",
"./node_modules/@oresoftware/package_b/dts"
]
```
package_a depends on package_b
(they are published to @oresoftware/package_a and @oresoftware/package_b, but you don't need to use NPM to reproduce the problem)
### Here is the problem
here is the source structure for package_a:
```
/src
a.ts
```
and a.ts looks like:
```
import * as x from 'lodash';
export {Foo} from '@oresoftware/package_b/dts/b'
console.log(x);
```
if you go into package_a, and run `tsc`, you will get this dist structure:
```
dist/
a.d.ts
a.js
```
and a.js looks like this:
```js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const x = require("lodash");
var b_1 = require("@oresoftware/package_b/dts/b");
exports.Foo = b_1.Foo;
console.log(x);
```
The *problem* is it's trying to load:
```
var b_1 = require("@oresoftware/package_b/dts/b");
```
at runtime, even though this is a `.d.ts` file!
My guess is that `tsc` just guesses that it's loadable,
because it could not find the file at compile time.
How to fix the problem!
If you cd into package_a and run `npm install`, and then run `tsc`, the problem goes away,
we now have this `dist/a.js` file:
```js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const x = require("lodash");
console.log(x);
```
so, idk if this is a bug or not.
In summary, if the .d.ts files are present `tsc` knows what they are obviously.
If the .d.ts files are missing, it assumes that they are .js files and generates
code to load those files at runtime which then breaks at runtime because
the files are not .js files.