具有扩展另一个类的类的打字稿定义文件(自动生成)

对于扩展另一个类的文件,自动生成的d.ts文件存在问题。我们有一个这样的类结构:(TS版本3.2.4)

export declare class MyBaseclass {
    id: string = '123';
    name: string = 'hello'
} 

然后我们有一个扩展此类的类:

import { MyBaseclass  } from 'some-absolute-path/resources/my-base-class'

export declare class MyClass extends MyBaseclass {
    someProperty: string = 'test';
    someFunction(): void {
        console.log('hello');
    };
}

然后使用此配置文件运行带有声明标志的tsc:

{
    "compilerOptions": {
        "target": "es5","module": "commonjs","sourceMap": true,"sourceRoot": "src","baseUrl": "src","moduleResolution": "node","emitDecoratorMetadata": true,"experimentalDecorators": true,"noEmitHelpers": false,"stripInternal": true,"declaration": true,"declarationmap": true,"outDir": "dist/commonjs","lib": ["esnext","dom"],"resolveJsonmodule": true
    },"exclude": ["node_modules",".vscode","dist","**/__tests__/*"]
}

d.ts文件似乎生成得很好:

my-base-class.d.ts

export declare class MyBaseclass {
    id: string;
    name: string;
}

my-class.d.ts

import { MyBaseclass  } from 'some-absolute-path/resources/my-base-class'

export declare class MyClass extends MyBaseclass  {
    someProperty: string;
    someFunction(): void;
}

然后我们将其打包为npm软件包并将其安装在单独的项目中。

在消费项目中使用MyClass时,我们无法访问MyClass的任何基本成员,例如

const test = new MyClass();
test.id <----- Compile time Error in editor: TS2399 Property 'id' does not exist on type 'MyClass'

我显然在这里丢失了一些东西。我在这里做错了什么?任何帮助将不胜感激。

谢谢

编辑: 这似乎与npm软件包中的绝对路径和相对路径有关。库中有绝对路径,并且d.ts文件也使用绝对路径生成。似乎在其他应用程序中使用这些类时,似乎无法通过其路径找到导入的类。我入侵了node_modules d.ts文件作为使用项目中的相对导入路径,并且工作正常。我也可以将npm软件包中的路径更改为相对路径,但是我想避免这种情况。

xinguozh 回答:具有扩展另一个类的类的打字稿定义文件(自动生成)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3059250.html

大家都在问