2016-07-06 25 views
0

夫婦在如何獲得自定義類型在anuglar2 quickstart項目中工作的事情。第三方應用程序級別臨時類型

我以outlayerhttps://github.com/metafizzy/outlayer爲例。

我已配置systemjs加載依賴關係。它可以和能與import * as outlayer from "outlayer";但我仍然得到編譯錯誤Cannot find module 'outlayer'.

爲了解決這個問題增加了一個分型文件outlayer.d.ts直接向node_modules/outlayer目錄,並更新了package.json包括"typings": "./outlayer.d.ts",

//outlayer.d.ts 

declare module Outlayer { 

    function create(str:string):any; 
} 

export = Outlayer; 

將此文件直接添加到node_module並不理想,原因很明顯。無需分叉圖書館,處理這個問題的更好方法是什麼?更好的是,我怎樣才能在應用程序級別獲得簡單/快速的打字,這樣我就不用擔心馬上寫出整件事情了?例如,如果我試圖現在使用data函數,我會看到Property 'data' does not exist on type 'typeof Outlayer'

總之,只需要能夠編寫一個自定義應用程序級別的類型,它不具有所有的好處,但也不會全部退出錯誤。一種臨時打字方式。

謝謝!

回答

1

以下是你需要做的:

  1. 下創建某處的文件(如custom-typings/outlayer.d.ts
  2. 使它成爲一個腳本文件(全球分型):

    declare module 'outlayer' { 
        function create(str: string): any; 
    } 
    
  3. 安裝與typings install file:custom-typings/outlayer.d.ts --global

看看是否有幫助。

編輯:更新的步驟3上面添加--global

+0

的偉大工程,只是需要用--global選項來安裝。 – Mike

+0

已更新的答案。 – unional