2017-06-06 44 views
-1

Typescript不支持許多WebGL 2功能和接口,如createVertexArray()。正因爲如此,我想編寫一個包裝函數的聲明文件,以便編譯器不會發生任何變化。要做到這一點,我寫了一個包含了不支持的功能的JavaScript文件:很多關於聲明文件的混淆

function createVertexArray(glContext) { 
    return glContext.createVertexArray(); 
} 
// a bunch more 

我現在需要爲這些功能的打字稿聲明文件。據我瞭解,當你要求打字稿加載一個模塊時,它會查看index.d.ts文件的目錄。你必須定義JavaScript文件中使用的函數和接口。這是我到目前爲止有:

declare module Webgl2 { 
    export interface WebGLVertexArrayObject {} 
    export function createVertexArray(glContext: WebGLRenderingContext):WebGLVertexArrayObject; 
    export function deleteVertexArray(glContext: WebGLRenderingContext, vertexArray: WebGLVertexArrayObject):void; 
    export function isVertexArray(glContext: WebGLRenderingContext, vertexArray: WebGLVertexArrayObject):boolean; 
    export function bindVertexArray(glContext: WebGLRenderingContext, vertexArray: WebGLVertexArrayObject):void; 
} 

export = Webgl2; 

不過,我需要用以下幫助:

  1. 如何加載模塊在我的打字稿應用的其餘部分?
  2. JavaScript文件是自動加載還是必須在頁面提供時手動執行?
  3. 在我的JavaScript文件中,我是否必須聲明函數作爲模塊導出?
  4. 在我的javascript文件中,我是否必須聲明createVertexArray()返回的接口?

我已經把這兩個文件放在pastebin here

+0

https://stackoverflow.com/help/how-to-ask –

+0

你如何建議我應該問這個問題? –

+0

首先我建議你在互聯網上做一些研究。你真的認爲你是世界上第一個試圖導入/導出模塊的人嗎? –

回答

0

有人已經在此工作了一段時間了。您可以找到WebGL2.d.ts文件here

然後,您可以使用tsconfig.json文件中的"files"字段導入,如here所示。

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "target": "es5", 
     "noImplicitAny": true, 
     "strictNullChecks": false, 
     "types": [], 
     "forceConsistentCasingInFileNames": true, 
     "lib": ["ES2015", "ES2017.SharedMemory", "DOM"] 
    }, 
    "files": [ 
     "Misc.d.ts", 
     "WebGL2.d.ts" 
    ] 
}