lundi 30 août 2021

Typescript: Module imported from another package with absolute path resolves any

I'm using VSCode and Typescript 4.3.5

my project structure is like this:

project
├── module-a
│   ├── src
│   │   ├──model
│   │   │  └── hello.ts
│   │   └── index.ts
│   └── tsconfig.json
└── module-b
    ├── src
    │   └── index.ts
    └── tsconfig.json

tsconfig:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "baseUrl": "./src",
    "rootDir": "../"
  },
}

module-a/src/model/hello.ts:

export const hello = () => console.log('hello');

module-a/src/index.ts:

import { hello } from 'model/hello'

const a = {
  hello,
  text: 'world'
}

export default a

module-b/src/index.ts

import a from '../../module-a/src'

type A = typeof a

/**
type A = {
  hello: any;
  text: string;
}
*/

Here's the problem, type A resolves { hello: any; text: string; }, and if I change the import in module-a/src/index.ts to a relative path:

import { hello } from './model/hello'

then type A is correctly resolved to { hello: () => string; text: string }

Is this a bug or expected?




Aucun commentaire:

Enregistrer un commentaire