I have a bunch of JavaScript scripts that I want to improve using JSDoc and TypeScript Typings
I need to be able to compile them absolutely independently so the code from one script doesn't mess up the typing system in the other script.
script1.d.ts
export {};
declare global {
var myGlobalVariable: {
property1: {
childProperty1: string;
}
}
}
script2.d.ts
export {};
declare global {
var myGlobalVariable: {
property1: {
childProperty2: string;
}
}
}
script1.js
/// <reference path="script1.d.ts" />
console.log(myGlobalVariable.property1.childProperty1);
script2.js
/// <reference path="script2.d.ts" />
console.log(myGlobalVariable.property1.childProperty2);
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"isolatedModules": true
},
"include": [
"*.js"
]
}
Executing
> tsc
I am getting the errors
script2.d.ts(4,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'myGlobalVariable' must be of type '{ property1: { childProperty1: string; }; }', but here has type '{ property1: { childProperty2: string; }; }'.
script2.js(3,40): error TS2551: Property 'childProperty2' does not exist on type '{ childProperty1: string; }'. Did you mean 'childProperty1'?
Is there a way to configure TypeScript to build my isolated modules indeed in an isolated way?
Additionally I prepared a gist with all those files mentioned above.
UPD
Also raised the same issue in the TypeScript repo
UPD2
To clarify confusions I will provide more context why do I need such architecture.
I am using Obsidian note-taking app with the Dataview plugin.
This plugin allows to execute custom JavaScript
```dataviewjs
await dv.view('script1');
```
which will invoke abovementioned script1.js
Internally Dataview executes script1.js
by reading its content and eval
s it.
Naturally, script1.js
and script2.js
are executed and eval
ed independently and have no shared context.