Do I have to reference typescript definition in every file

前端 未结 5 1950
囚心锁ツ
囚心锁ツ 2020-12-02 16:24

Is there a way to tell typescript to use a certain file (or set of files) as definition for everything compiled?

My only alternative currently is to add something li

5条回答
  •  青春惊慌失措
    2020-12-02 17:13

    What I've learned so far is that /// < reference >-ing a module with reference comments is not a good method.

    For example: in case you have a file Foo and a file Bar. Both files use jquery, but only file Foo has a reference comment to jquery. If file Foo is deleted for some reason, your file Bar is broken, because the reference is missing.

    If you are using TypeScript >= 2.0 It is better to define the TypeScript definition files (.d.ts) in your tsconfig.json under the "files" section.

    This could look like this:

    {
      "compileOnSave": true,
      "compilerOptions": {
        "noImplicitAny": true,
        "noEmitOnError": true,
        "removeComments": false,
        "sourceMap": true,
        "target": "es5", 
        "outDir": "./Scripts/"
      },
      "files": [
        "./src/foo.ts",
        "./src/bar.ts",
        "./Scripts/typings/jquery/jquery.d.ts",
        "./Scripts/typings/jqueryui/jqueryui.d.ts",
        "./Scripts/MicrosoftMaps/Microsoft.Maps.d.ts"
      ]
    }
    

    Using the /// directive (reference comments) is often used in examples to get you started quickly, but it is not a best practice. Also many examples come from a version < TypeScript 2.0.

提交回复
热议问题