简体   繁体   English

打字稿2:node_modules中的自定义类型定义

[英]Typescript 2: Custom Type definition in node_modules

Let's say I publish a custom library called my-library . 假设我发布了一个名为my-library的自定义my-library This library will be of the structure 该库将具有以下结构

node_modules/
    my-library/
        lib/
            index.js
            src/
              ...
        typings/
            index.d.ts
        package.json

Say now I export default something called Application from the index.js, so you use it by import Application from 'my-library'; 现在说我从index.js导出默认的名为Application东西,因此您可以通过import Application from 'my-library';使用它import Application from 'my-library';

Now, in my index.d.ts I want to declare its typing: 现在,在我的index.d.ts我想声明其类型:

export = Application;
export as namespace Application;

declare namespace Application {
   function foo(arg1: string): object;
}

I know expect this to return an error but it does not: 我知道这会返回错误,但是不会:

import Application from 'my-library';

Application.foo(123)[0].blahblah;

Not only is the argument wrong, but so is the use of the return value. 该参数不仅错误,而且返回值的使用也错误。 I don't know how I am supposed to add my type definitions so that the above works. 我不知道应该如何添加我的类型定义,以便上面的工作。

You can look here to see where Typescript looks for definitions. 您可以在此处查看Typescript在何处查找定义。

What you probably want is to add a types in your package.json to point to a custom definition file. 您可能想要的是在package.json添加一个types ,以指向自定义定义文件。

{
    "types": "typings/index.d.ts",
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM