简体   繁体   English

TypeScript编译错误-类型上不存在属性

[英]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类的每个公共成员。


  1. It is only "somewhat" equivalent to an 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.

相关问题 Typescript编译错误类型“ LodashArrayWrapper”不存在属性“ some” <any[]> &#39; - Typescript compile error Property 'some' does not exist on type 'LodashArrayWrapper<any[]>' 打字稿编译错误:类型“节点”上不存在属性“classList” - Typescript compile error: Property 'classList' does not exist on type 'Node' Typescript编译错误:类型&#39;typeof e&#39;不存在属性&#39;bodyParser&#39; - Typescript compile error: Property 'bodyParser' does not exist on type 'typeof e' 打字稿错误属性在类型上不存在 - Typescript error Property does not exist on type 打字稿中的错误~类型void~中不存在属性 - Error in typescript ˜Property does not exist in type void˜ ReactJS 中的 Typescript 错误 - 类型上不存在属性 - Typescript error in ReactJS - Property does not exist on type 打字稿类型错误属性不存在 - Typescript type error property does not exist TypeScript泛型错误:类型上不存在属性 - TypeScript generics error: Property does not exist on type 然后,属性在void类型上不存在,打字稿错误 - property then does not exist on type void , A typescript error Typescript错误属性&#39;members&#39;在类型&#39;{}&#39;上不存在 - Typescript Error Property 'members' does not exist on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM