Javascript “来自超类的方法”;“不存在”;打字。。。。?
我们有一个TS应用程序,它使用一个JS库,我们在其中构建了一个.d.TS文件,将其与TypeScript一起使用。 当我们将该文件放在项目中的“typings”文件夹中时,一切正常。然而,我们决定将这些类型转移到单独的回购协议中,以便与其他项目共享 问题:将类型移动到外部repo后,Idea(Idea和VSCode)不再识别超级类型的属性和方法。该应用程序可以编译、运行,所有功能都可以正常工作。但是IDE一直说这些文件有错误 有什么想法吗 键入repo结构如下所示:Javascript “来自超类的方法”;“不存在”;打字。。。。?,javascript,typescript,inheritance,typescript-typings,Javascript,Typescript,Inheritance,Typescript Typings,我们有一个TS应用程序,它使用一个JS库,我们在其中构建了一个.d.TS文件,将其与TypeScript一起使用。 当我们将该文件放在项目中的“typings”文件夹中时,一切正常。然而,我们决定将这些类型转移到单独的回购协议中,以便与其他项目共享 问题:将类型移动到外部repo后,Idea(Idea和VSCode)不再识别超级类型的属性和方法。该应用程序可以编译、运行,所有功能都可以正常工作。但是IDE一直说这些文件有错误 有什么想法吗 键入repo结构如下所示: -custom-typing
-custom-typings
- index.d.ts
- tsconfig.json
- package.json
package.json:
{
"name": "custom-typings",
"author": "Author Name",
"version": "0.1.3",
"types": "./index.d.ts",
"dependencies": {
"typescript": "next"
}
}
tsconfig:
{
"compilerOptions": {
"target": "ES2015",
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"esModuleInterop": true,
"alwaysStrict": true,
"lib": [
"ES2015",
"ES2016",
"dom"
],
"pretty": true,
"moduleResolution": "node",
"module": "ES2020"
},
"exclude": [
"dist",
"node_modules",
],
"typeAcquisition": {
"enable": true
}
}
索引d.ts
declare module 'custom-typings' {
export class A {
aMethod();
}
export class B extends A{
bProperty: any = {foo: 'Foo'};
}
const CustomTypes: {
B: typeof B;
};
export default CustomTypes
}
使用以下类型:
import CustomTypes from 'custom-typings';
class MyComponent extends CustomTypes.B {
init() {
console.log(this.bProperty.foo); // property 'bProperty' does not exist in type MyComponent
this.aMethod(); // property 'aMethod' does not exist in type MyComponent
}
}
因此,“自定义类型”回购协议的结构恰好阻止了项目解析类型 我们正在为声明了自己模块的现有库编写类型定义。我们的repo声明的模块与lib的模块同名,我们称之为“lib模块” repo类型需要包含一个名为“lib module”的目录,并用相同的名称声明模块:
declare module "lib-module" {
// properties and functions here
}
新的结构是:
-custom-typings
- lib-module
- index.d.ts
- tsconfig.json
- package.json
这样,任何项目都可以导入以下类型:
import CustomTypes from 'lib-module';
B
似乎不在范围内,您没有从中得到错误吗?嗨@Bergi。刚刚编辑过。它实际上是定制的。我得到的唯一错误就是上面提到的那个。我们可以通过按住CTRL键并单击这些类型来导航到它们。Syntax会突出显示,但始终会显示此消息。模块的默认导出是一个对象,该对象被声明为仅具有C
属性-这应该不起作用。尝试从“…”以自定义类型的形式导入“…”或从“…”导入“{B}”代码>相反,我再次抱歉。。刚刚更新过。它实际上是定制类型。我不能在这里发布实际代码,所以我在“翻译”中错过了这一点,啊,刚才看到了你的编辑。我认为问题在于C
实际上与B
不同。Typescript可能知道构造函数的类型与B
构造函数的类型相同,但它们仍然是不同的。