2015-05-15 15 views
6

Lodash'da _.map ile kullanılacak bir sözlük nasıl bildirilir?Typescript lodash: _.map ile kullanılacak bir sözlük nasıl bildirilir?

İşte örnek TypeScript programı.

<reference path="../scripts/typings/lodash/lodash.d.ts" /> 
interface IQuestionAndOptions { 
    text: string; 
    options: { [key: number]: string }; 
} 

function sample() { 
    var question: IQuestionAndOptions = { text: "Are you happy?", options: {} }; 
    question['1'] = "Yes"; 
    question['2'] = "No"; 

    var values = _.map(question.options, function (v: string, k: number) { 
    return { text: v, value: k }; }); 
} 

Bu, question.options bildiriminden memnun değildir ve aşağıdaki hatayı verir.

"{[key: number] türünde argüman: string; ,

map<T, TResult>(
     collection: Array<T>, 
     callback: ListIterator<T, TResult>, 
     thisArg?: any): TResult[]; 

Bir dizi için collection değişken gerektirir:} map yöntem DefinitelyTyped aşağıdaki gibi tanımlanmıştır

cevap

0

Sorun, lodash.d.ts ile oldu ve güncelleştirme sorunu çözüldü.

Nuget lodash.TypeScript.DefinitelyTyped version = "0.3.8"

map<T, TResult>(
    collection: List<T>, 
    pluckValue: string): TResult[]; 

tanımını ve eserlerini kullanır.

0

_.map komut 'List tipi parametreye atanabilir değildir' Bu, { [key: number]: string }'dan daha spesifiktir. Çalışması için options alanını bir dizi olarak bildirmeniz gerekir.