Hello,
I'm trying to use ts-node to workaround an issue in jest that doesn't allow globalSetup and globalTeardown using typescript.
My project compiles correctly using tsc, now, when I use ts-node to require the code, my global files seem to be ignored.
I have globals.d.ts and globalsAugmented.d.ts.
In globals.d.ts I do multiple global declare type and in the globalsAugmented.d.ts I define other global interfaces that depend on other modules I have in the system.
My project is splited in two different tsconfig.json files. My main one and a secondary for the tests. The tests tsconfig extend the main one. Could this be generating issues ? As I mentioned before, tsc is able to compile without issue with both of them.
Thanks!
Please see the README:
Tip: ts-node differs slightly from tsc. It will not load files/includes/excludes from tsconfig.json by default. Instead, ts-node starts from the input file and discovers the rest of the project tree through imports and references. TypeScript provides types and path mapping functionality to resolve definitions.
I can only assume without seeing any code that this is the issue.
Totally missed that in the README.
Is there any workaround?
Thanks!
Martín
On Sun, Jul 08, 2018 at 20:25 Blake Embrey < Blake Embrey ( Blake Embrey notifications@github.com ) > wrote:
Closed #635 ( https://github.com/TypeStrong/ts-node/issues/635 ).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub (
https://github.com/TypeStrong/ts-node/issues/635#event-1722032606 ) , or mute
the thread (
https://github.com/notifications/unsubscribe-auth/ACGV9y9UNn6KfdcLc1B6DS0CqrLHnUQ6ks5uEpTSgaJpZM4VG0fz
).
Why wouldn't you use path mapping or types? Those are the solutions TypeScript envisioned to solve what you want to do. There's also triple-slash references.
If all those aren't suitable to you, there's this:
--files Load files from tsconfig.json on startup (TS_NODE_FILES, default: false)
@blakeembrey Thanks for the reply again!
Correct me if I'm wrong please, but, path mapping and types would require import statements where needed right ? The purpose of the two files I mentioned before is that I don't need to import them, the types are available globally. I know this is not the a good pattern but is what we have at the moment and I didn't want to refactor that at this moment.
Types are for your globals and path mapping for imports. I’m sure there’s better information in the links in the README though since it points to the TypeScript docs. It does, however, require a specific structure for the types to be detected automatically and used.
@blakeembrey Thanks for the quick reply again!
I think I got your point. I basically need to explicitly declare in the types array the path to globals.d.ts and globalsAugmented.d.ts right ? I would also need to add the node_modules/@types and node_modules so that I don't loose the default behaviour.
Thanks again!
:)
I'll work on updating the README with better examples today 👍
Hey, even with the Readme, I can't get around augmenting global.
I am using Loopback which have really bad typings, and was able to augment everything in this modules without any troubles.
However, in our app, we have global.app which is shared everywhere, so I need to be able to augment the Global interface.
Previously I was doing:
declare namespace NodeJS {
interface Global {
app: any
}
}
however with the new system I can't get that to work, even embedded into a subfolder in ./types
tsconfig:
...
"typeRoots": [
"./types",
"./node_modules/@types"
]
...
@Tronix117 how did you figure it out?
@IgorSzymanski I'm not sure what I did, but it is working... Here's what I got
./types/app/index.d.ts
declare global {
namespace NodeJS {
interface Global {
app: any
}
}
}
./ts-config.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"typeRoots": [
"./types",
"./node_modules/@types"
]
},
"exclude": [
"node_modules"
]
}
By naming the file index.d.ts and put it inside the app folder solved the issue
Most helpful comment
@IgorSzymanski I'm not sure what I did, but it is working... Here's what I got
./types/app/index.d.ts
./ts-config.json