Where/how to add d.ts for private, scoped npm modules?

后端 未结 1 1735
难免孤独
难免孤独 2021-01-15 21:10

I am attempting to port a Javascript project to Typescript. I have a dependency named like: @myscope/utils

A file from this is imported into the ts file

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-15 21:48

    I managed to solve this myself. I referred to the information on writing declaration files

    The key to get the module to be resolved is to declare a module with a name of the full path of the package. I created a index.d.ts file for the module, that I added to my files section of tsconfig.json.

    The file contained the following declaration:

    declare namespace date {
      function now(): number;
    }
    
    declare module "@myscope/utils/date" {
       export = date; 
    }
    

    Though not necessary, I structured my imports like typings does. I may simplify later after I've ported the rest of the application.

    I created the following files:

    mytypings/index.d.ts
    mytypings/modules/@myscope/utils/index.d.ts
    

    Note there is no date part of the utils path.

    mytypings/index.d.ts contains a reference path to mytypings/modules/@myscope/utils/index.d.ts and then was added to section of tsconfig.json.

    My plan is to add other libraries in the same way.

    0 讨论(0)
提交回复
热议问题