How can I write and use custom declaration files that do not exist on DefinitelyTyped?

I am using an npm package with a name foothat does not exist on DefinitelyTyped. In other words, @types/foodoes not exist.

I would still like to use it with more stringent settings, such as noImplicitAny, so I need to write custom definition files myself. In the end, I would like to send a pull request for DefinitelyTyped so that this file is useful to other users outside my project.

There are such simple solutions as creating a global file with a name ./src/types.d.tswhere I can write the following

declare module "foo" {
    export function hello(): void;
    export function world(): void;
}

But if I use this syntax, I will probably have to rewrite my module when I send it to a specific type.

How can I structure my project so that I can easily create an author and then send local .d.ts files to DefinitelyTyped?

+4
source share
1 answer

I will do this step by step with explanations, so it may seem long, but according to the instructions that must be followed, it only takes a few minutes .

Imagine the following project structure:

proj/
├─ tsconfig.json
└─ src/
   └─ ...

Create a local folder in the root of your project.

It does not matter what he called, but we will call him local-types. You can change it to whatever you want if you have a good understanding of what we have done here.

proj/
├─ tsconfig.json
├─ local-types/
└─ src/
   └─ ...

types.

tsconfig.json.

  • ( Node)
  • baseUrl
  • (.. paths)
  • typeRoots

tsconfig.json :

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "strict": true,
        "outDir": "./dist",


        /*****************************
         * Module Resolution Options *
         *****************************/

        // Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).
        "moduleResolution": "node",

        // Base directory to resolve non-absolute module names.
        "baseUrl": "./",

        // A series of entries which re-map imports to lookup locations relative to the 'baseUrl'.
        "paths": {
            "*": ["local-types/*"]
        },

        // List of folders to automatically include type definitions from.
        "typeRoots": [
            "node_modules/@types",
            "node_modules/",
            "local-types"
        ]
    },
    "include": [
        "src"
    ]
}

:

  • baseUrl - , paths. , local-types/* ./local-types/*.
  • paths local-types.
  • typeRoots " ". , Node Mocha.

.

foo, foo index.d.ts:

export function hello(): void;

export function world(): void;

:

proj/
├─ tsconfig.json
├─ local-types/
|  └─ foo/
|     └─ index.d.ts
└─ src/
   └─ ...

foo `src.

import { hello, world } from "foo";

hello();
world();

, index.d.ts. , npm.

DefinitelyTyped

.d.ts ? , pull DefinitelyTyped, !

, .

  • declare module "..." .
    • ... . , , .
  • , import export, script/ .
  • , - TypeScript .
+7

Source: https://habr.com/ru/post/1680280/


All Articles