Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ts node - Typescript does not grab custom types from the referenced project

My project structure is next (I'm running code with ts-node)

project-a
    tsconfig.json // referencing 'common'
    specs/
        test.ts // import 'helper.js' from "../../common/utils/helper.js"

project-b
    tsconfig.json // has my own custom types in the 'types'
    lib-types/
        global/
            index.d.ts // defenition what is 'foo' is here
    utils/
        helper.js // this file has global var 'foo' in it

utils.js (project-b)

global.foo = 'Hello!'

function helper() {
    return foo
}

module.exports = {helper}

test.ts (project-a)

import {helper} from "../../common/utils/helper"
import {assert} from 'chai'

describe('Test block', ()=> {
    it('Test', ()=> {        
        assert.strictEqual(helper(), 'Hello!', 'Should greet')
    })
})

Thanks to "types": ["./lib-types/global"] in the project-b tsconfig.json TS is aware of what foo and is working fine inside project-b folder.

But when I try to reference proejct-a to project-b, first project throws an error:

TSError: ? Unable to compile TypeScript:
../common/utils/helper.js(1,8): error TS2339: Property 'foo' does not exist on type 'Global & typeof globalThis'.
../common/utils/helper.js(4,12): error TS2304: Cannot find name 'foo'.

Configs:

project-a tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "strict": false,
        "outDir": "./build",
        "types": ["mocha", "node", "chai"],
        "skipLibCheck": true,
        "esModuleInterop": true,
        "module": "commonjs",
        "skipDefaultLibCheck": true
    },
    "include": [
        "specs/**/*"
    ],
    "references": [
        {
            "path": "../common",
        }
    ]
}

project-b tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "strict": false,
        "outDir": "./build",
        "types": [
            "node", 
            "./lib-types/global"
        ],
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "module": "commonjs",
        // composite options
        "composite": true,
        "declaration": true,
        "declarationMap": true
    },
    "include": ["utils/**/*"]
}

I assume I'm doing something wrong while referencing projects.

Or I just didn't got the idea of the reference right...

Is it possible to make project-a be aware of the own custom types of project-b WITHOUT explicitly specifying the "../common/types/global" in project-a folder?

p.s. I'm aware of extends and shared configs but this is not a case for me

UPD:

Not sure is this important but here is how tests are run:

"scripts": {
    "test": "mocha './specs/**/*.test.js' './specs/**/*.test.ts' --file ./ts.runner.js"
  },

ts.runner.js

require("ts-node").register({ files: true })

question from:https://stackoverflow.com/questions/65829212/typescript-does-not-grab-custom-types-from-the-referenced-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...