2017-09-07 7 views
0

typestypesのTypescriptコンパイラが約束チェーンのカリングに同意していないような場合は、ramdaの使い方に少し問題があります。アウトバウンドAPI要求をスタブする簡単な例を示します。makeRequestは、アウトバウンド要求を行い、本文を返すヘルパー関数です。Typescriptで約束を持つジェネリックを使わないで

import * as R from 'ramda'; 

interface Item { 
    [k: string]: string; 
} 

interface OtherResponse { 
    key: string; 
    value: string; 
} 

type ItemResponse = Item[] | Item[][] | OtherResponse; 

function makeRequest(): Promise<ItemResponse> { 
    // In the real world, this would make an outbound request, and return one of the 3 responses depending on parameters to makeRequest. 
    return Promise.resolve([[{foo: 'bar'}]]); 
} 

function getNestedItems(): Promise<Item[]> { 
    return (makeRequest() as Promise<Item[][]>) 
    .then(R.flatten); 
} 

getNestedItems() 
    .then(console.log) 
    .catch(console.error); 

は今だけ、この部分に焦点を当て:

function getNestedItems(): Promise<Item[]> { 
    return (makeRequest() as Promise<Item[][]>) 
    .then(R.flatten); 
} 

現在、これは私に次のエラー(試み1)が得られる:迷惑なんだ

TS2322: Type 'Promise<(Item | Item[])[]>' is not assignable to type 'Promise<Item[]>'. Type '(Item | Item[])[]' is not assignable to type 'Item[]'. Type 'Item | Item[]' is not assignable to type 'Item'. Type 'Item[]' is not assignable to type 'Item'. Index signature is missing in type 'Item[]'. 

を、私はちょっと理由を理解します許可されていないので、試み2

私は、次しまった場合を除き
function getNestedItems(): Promise<Item[]> { 
    return (makeRequest() as Promise<Item[][]>) 
    .then(data => R.flatten(data)); 
} 

TS2322: Type 'Promise<Item[][]>' is not assignable to type 'Promise<Item[]>'. Type 'Item[][]' is not assignable to type 'Item[]'. Type 'Item[]' is not assignable to type 'Item'. Index signature is missing in type 'Item[]'. 

は今、これは活字体コンパイラがR.flattenが使用されているという事実を無視して、単に間違っています。私はなぜこれが起こるか把握できませんでした。私の最後の試みは、このでした:これは私には動作しますが

function getNestedItems(): Promise<Item[]> { 
    return (makeRequest() as Promise<Item[][]>) 
    .then(data => R.flatten<Item>(data)); 
} 

ちょうど醜いです、そしてRAMDAのカリー化から来るの利益の多くを削除します。

ラムダ関数を使用するたびにジェネリックを使用する必要がないTypescriptのPromiseチェーン付きRamDAを使用するための、より標準的な方法がありますか?

EDIT:

tsconfig.json興味を持っている場合:NPM --saveをインストール: ラン:

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "noEmitOnError": true, 
    "noImplicitAny": true, 
    "noImplicitReturns": true, 
    "noImplicitThis": true, 
    "noUnusedLocals": true, 
    "noUnusedParameters": false, 
    "outDir": "dist", 
    "preserveConstEnums": true, 
    "removeComments": true, 
    "rootDir": ".", 
    "sourceMap": false, 
    "strictNullChecks": true, 
    "target": "es2015" 
    }, 
    "exclude": [ 
    "node_modules" 
    ], 
    "include": [ 
    "src/**/*.ts*", 
    "test/**/*.ts*" 
    ] 
} 

答えて

-1

プロジェクトで追加するには、ここでhttps://github.com/DefinitelyTyped/DefinitelyTyped

からRAMDAのタイピングを得ることができます-dev @ types/ramda

希望すると助かります!

+0

私はあなたが質問を読むとは思わない。私はpromise chainでRamdaを正しく使う方法を尋ねています。私は投稿の冒頭で私が使っているタイプを指定します。 – Dustin

関連する問題