【问题标题】:Array.prototype.find Interface Extension raise issues when including ModuleArray.prototype.find 接口扩展在包含模块时引发问题
【发布时间】:2016-09-17 23:48:55
【问题描述】:

就我学习 Typescript 而言,我遇到了一个问题。 首先,我在我的单个 app.ts 中实现了这个Array Extension。由于多种原因,我正在这样做!

我的项目代码越来越大,我决定创建我的第一个模块。我创建了一个名为 whatever.ts 的新文件:

export class Foo{...}

我将这个模块导入到我的 app.ts 中,如下所示:

import * as whatever from './whatever'
let Foo = new whatever.Foo();

但在这一点上,我之前的 Array Extension 看起来给我的旧代码带来了一些麻烦。

class KeyValuePair<T, U> {
    public key: T;
    public value: U;

    constructor(key: T, value: U) {
        this.key = key;
        this.value = value;
    }
}

class Dictionnary<T, U> {
    private _keyValuePairs: Array<KeyValuePair<T, U>>;

    constructor() {
        this._keyValuePairs = new Array<KeyValuePair<T, U>>();
    }
...
}

每次我使用数组函数/方法时,intellisense 都会尖叫:

[ts] 类型“数组>”上不存在属性“xxx”。

任何

每次我尝试使用时都会发生这种情况:

this._keyValuePairs.splice(...
this._keyValuePairs.indexOf(...
this._keyValuePairs.length
for (let kvp of this._keyValuePairs) {...

如果您需要示例,请尝试使用此方法:(也有其他广告在此方法上)

public keys(): Array<T> {
    let keys = new Array<T>();

    for (let kvp of this._keyValuePairs) {
        keys.push(kvp.key);
    }
    return keys;
}

如果我不导入模块,一切都好。我的第一个模块失败了吗?这个问题已知吗? (:我没有在上面找到任何东西。)或者他们是用多个文件对我的应用程序进行编码的好方法,或者我只是卡住了,只要我使用这个数组,就必须在一个文件中编码。 prototype.find 扩展?

谢谢。

【问题讨论】:

  • 你的代码有 Plunk 吗?
  • 当然,试试这个one

标签: typescript intellisense visual-studio-code typescript1.8


【解决方案1】:

使用 tsconfig.json,像这样针对 es5:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "module": "commonjs", 
        "target": "es5"
    }
    ...

【讨论】:

  • 你很快就放弃了,得出了一个错误的结论。
  • 这只是一个简单的解决方法,请查看stackoverflow.com/questions/31455805/… 了解更多信息。或者用你自己的解决方案来回答会很有用。
猜你喜欢
  • 2016-02-24
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多