【问题标题】:Passing an object to ._map in typescript在打字稿中将对象传递给 ._map
【发布时间】:2017-01-03 12:51:55
【问题描述】:

下面是我的静态成员配置类。

import {Injectable} from '@angular/core';
import {Config} from '../shared/interfaces'

export class GlobalData {

    static config:Config = {
        appName: "",
        line: "",
    }

    constructor(private app:PApplication){
        GlobalData.config.appName = app.getData().Style? 'P_APP' : 'P_WEB'
        GlobalData.config.line = 'PApp'
    }

}

下面是我的界面配置

export interface Config {
    appName: string
    line: string
}

我正在尝试将我的静态配置对象传递给以下函数:

var appConfig = this.appConfig(GlobalData.config);

appConfig = (configVariable: Config) => {
    return _.map(configVariable, function(val, key) {
        return key + "=" + val;
    }).join("&");
}

我收到以下错误:

[ts] 'Config' 类型的参数不可分配给类型参数 '列表 |字典 |数字字典'。输入“配置” 不可分配给类型“NumericDictionary”。 “配置”类型中缺少索引签名。

基本上在Javascript 中,配置是global object,可以在任何controller/services 中访问。

var config = {appName: "", line: "" }

【问题讨论】:

    标签: javascript angular typescript lodash


    【解决方案1】:

    map function are 的定义:

    map<T, TResult>(
        list: _.List<T>,
        iterator: _.ListIterator<T, TResult> | _.IterateePropertyShorthand | _.IterateeMatcherShorthand<any>,
        context?: any): TResult[];
    
    map<T, TResult>(
        object: _.Dictionary<T>,
        iterator: _.ObjectIterator<T, TResult>,
        context?: any): TResult[];
    

    如您所见,传递的对象应该是_.List&lt;T&gt;_.Dictionary&lt;T&gt; 类型。
    在您的情况下,它需要是 _.Dictionary&lt;T&gt;,其定义如下:

    interface Dictionary<T> extends Collection<T> {
        [index: string]: T;
    }
    

    您的Config 接口没有索引签名,这就是编译器抱怨的原因。
    要解决这个问题,只需将其转换为 any:

    return _.map(configVariable as any, function(val, key) {
        return key + "=" + val;
    }).join("&");
    

    编辑

    lodash 中map 的签名类似:

    map<T, TResult>(
        collection: List<T>,
        iteratee?: ListIterator<T, TResult>
    ): TResult[];
    
    map<T extends {}, TResult>(
        collection: Dictionary<T>,
        iteratee?: DictionaryIterator<T, TResult>
    ): TResult[];
    
    map<T extends {}, TResult>(
        collection: NumericDictionary<T>,
        iteratee?: NumericDictionaryIterator<T, TResult>
    ): TResult[];
    
    map<T, TResult>(
        collection: List<T>|Dictionary<T>|NumericDictionary<T>,
        iteratee?: string
    ): TResult[];
    
    map<T, TObject extends {}>(
        collection: List<T>|Dictionary<T>|NumericDictionary<T>,
        iteratee?: TObject
    ): boolean[];
    

    Dictionary 有点不同:

    interface Dictionary<T> {
        [index: string]: T;
    }
    

    所以这是同样的问题。
    至于避免使用any,你可以这样做:

    interface Config extends _.Dictionary<string> {
        appName: string
        line: string
    }
    

    然后是这个:

    return _.map(configVariable, function(val, key) {
        return key + "=" + val;
    }).join("&");
    

    编译不会出错。

    【讨论】:

    • 谢谢,如何在不传递任何类型的情况下实际修复它...这里
    • 另外我正在使用 lodash.. 不是 underscore.js 未能更新。
    • 检查我修改后的答案
    • 谢谢...所以我们应该定义字典对吗? Typescript 默认不提供...
    • 对不起,应该是:_.Dictionary&lt;string&gt;。我也在我的答案中修复了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2019-10-31
    • 2017-09-23
    • 2020-06-12
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多