[英]TypeScript Compile Error - Property does not exist on type
When I am trying to compile my typescript code the following error appears: 当我尝试编译我的打字稿代码时,出现以下错误:
src/Project/ProjectCollection.ts:6:13 - error TS2339: Property 'add' does not exist on type 'BaseCollection'.
I have attached the code below as a demo: 我已附上以下代码作为演示:
ProjectCollection.ts ProjectCollection.ts
export default class ProjectCollection extends BaseCollection {
addProject(project: Project) {
super.add(project)
}
}
BaseCollection.ts BaseCollection.ts
// @ts-ignore
import Collection from 'collectionsjs';
export default abstract class BaseCollection extends Collection {
public all() {
return super.all();
}
}
I am using the following library on github for collections: ( https://github.com/logaretm/collectionsjs ) 我在github上使用以下库进行收集:( https://github.com/logaretm/collectionsjs )
If I swap the line from: 如果我从以下位置换行:
import Collection from 'collectionsjs'
to: 至:
const Collection = require('collectionsjs')
Then the above error no longer occurs but when running tslint ( https://palantir.github.io/tslint/ ) I get this error: 然后以上错误不再发生,而是在运行tslint( https://palantir.github.io/tslint/ )时出现此错误:
ERROR: src/BaseCollection.ts:1:20 - require statement not part of an import statement
Just wondering if someone could explain why the first issue with the using import is occurring and how both of these issues can be resolved simultaneously? 只是想知道是否有人可以解释为什么使用import出现了第一个问题,以及如何同时解决这两个问题?
Thanks 谢谢
Just wondering if someone could explain why the first issue with the using import is occurring and how both of these issues can be resolved simultaneously?
只是想知道是否有人可以解释为什么使用import出现了第一个问题,以及如何同时解决这两个问题?
The first issue is happening because the collectionsjs
module does not have a TypeScript declaration file. 发生第一个问题是因为
collectionsjs
模块没有TypeScript声明文件。 As a result, the TypeScript compiler does not have any information about the Collection
type. 结果,TypeScript编译器没有有关
Collection
类型的任何信息。 Even though at runtime Collection
will have a certain interface, to the compiler the Collection
type is somewhat 1 equivalent to this: 即使在运行时
Collection
具有一定的接口,对于编译器来说, Collection
类型在某种程度上也等于1 :
const Collection: any = {};
The way to resolve the issues is to add a type declaration for the collectionsjs
module and to create a type that describes what that package exports. 解决问题的方法是为
collectionsjs
模块添加类型声明,并创建一个描述该包导出内容的类型。
Here is a start of that type declaration file for you: 这是为您提供的该类型声明文件的开始:
collectionsjs.d.ts collectionsjs.d.ts
declare module 'collectionsjs' {
export default class Collection {
add(item);
all();
}
}
To finish the job, add each of the public members of the Collection
class . 要完成工作,请添加
Collection
类的每个公共成员。
any
type because though we can extend
the Collection
we cannot extend
the any
type. any
类型,因为尽管可以extend
Collection
但不能extend
any
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.