alt*_*tus 4 javascript node.js node-modules typescript type-definition
如果我将TypeScript模块另存为my-function.ts,如下所示:
export function myFunction (param: number): number { return param }
Run Code Online (Sandbox Code Playgroud)
它将以任何方式编译为JavaScript,并释放其类型定义。然后,我可以创建一个index.d.ts文件来声明此模块的定义,但是重新定义/重新定义这些定义似乎有点乏味。
有没有办法从my-function.ts文件自动生成类型定义到index.d.ts文件?
nad*_*avy 10
我的 tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./tsOutputs"
},
"include": [
"lib/**/*.ts",
"index.ts"
],
"exclude": [
"test/**/*.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
注意:为了能够投射和创建对象实例,每个实体需要有两个不同的导出。一次作为type,另一个作为const。
这是我的 index.ts:
import {HttpClient as HC} from "./lib/http/http-client";
import {HttpRequest as HReq, HttpResponse as HRes} from "./lib/http/contracts";
export namespace MyJsInfra
{
Run Code Online (Sandbox Code Playgroud)
export type HttpClient = HC;
export namespace Entities{
export type HttpRequest = HReq;
export const HttpRequest = HReq;
export type HttpResponse = HRes;
export const HttpResponse = HRes;
}
}`
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关此双重声明背后的推理的更多信息:https : //github.com/Microsoft/TypeScript/issues/10058#issuecomment-236458961
完成以下所有操作后,当我们运行 build 时,每种类型都应该有相应的“*.d.ts”文件。现在我们必须处理基础设施的package.json,以便打包所有项目。
在package.json 中确保设置types,main指向生成的index.d.ts和index.js文件。此外,您必须确保“*.d.ts”文件被打包为基础设施的一部分。就我而言,我在files属性中指定了以下模式:“tsOutputs/**/*.d.ts”
这是我的 package.json:
{
"name": "my-js-infra",
"version": "1.0.0",
"description": "Infrastructure code.",
"scripts": {
"build":"./node_modules/.bin/tsc -p .",
"prepublish":"npm run build",
},
"homepage": "https://github.com/Nadav/My.JS.Infra#readme",
"devDependencies": {
...
"typescript": "^2.4.2",
...
},
"dependencies": {
...
"needle": "^1.4.2",
...
},
"files": [
"tsOutputs/**/*.js",
"tsOutputs/**/*.d.ts",
"tsOutputs/index.d.ts"
],
"types":"tsOutputs/index.d.ts",
"main":"tsOutputs/index.js"
}
Run Code Online (Sandbox Code Playgroud)
全部完成。现在您可以发布您的公共代码。
npm install my-js-infra --save这是我的 tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "es6"],
"module": "umd",
"sourceMap": true,
"watch": false,
"outDir": "./tsOutputs",
"moduleResolution":"node" /* This must be specified in order for typescript to find the my-js-infra. Another option is to use "paths" and "baseUrl". Something like:
...
"baseUrl": ".", // This must be specified if "paths" is used.
"paths":{
"my-js-infra":["node_modules/my-js-infra/tsOutputs/index.d.ts"]
}
...
*/
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关Typescript中模块分辨率的更多信息:https : //www.typescriptlang.org/docs/handbook/module-resolution.html
import {MyJsInfra } from "my-js-infra";
public doMagic(cmd, callback) {
try {
var request:MyJsInfra.Entities.HttpRequest = {
verb: "GET",
url: "http://www.google.com",
};
var client = new MyJsInfra.HttpClient();
client.doRequest(request, (err, data)=>{
if (err)
return callback(err, null)
return callback(null, data);
})
} catch (err) {
callback(err);
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用该--declaration标志进行编译,TypeScript将自动.d.ts为您生成文件。
此模式将要求您可以看到某些类型,以便可以在.d.ts文件中对其进行描述。