【发布时间】:2020-10-20 10:43:33
【问题描述】:
我有一个 Web API,我想为它构建一个库,这样我就不需要在与这个 API 交谈的每个新项目中编写相同的代码。
我还想拆分我的代码,所以我为每个端点结构都有一个类,因此,例如,假设我有多个用户端点,它们可以做类似的事情
- 注册
- 登录
我会有一个 UserClient 提供两种方法
export default class UserClient
{
register(email: string, password: string)
{
// code here
}
login(email: string, password: string, remember: bool)
{
// code here
}
}
所以,我的代码结构是这样的
package.json
tsconfig.json
--src
----api
------user-client.ts
----models
------user
--------logindata.ts
我的 tsconfig.json 看起来像这样
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"module": "commonjs",
"noImplicitAny": true,
"lib": ["es2017", "es7", "es6", "dom"],
"outDir": "./dist",
"target": "es2015",
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"typedocOptions": {
"mode": "modules",
"out": "docs"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
还有我的 package.json
{
"name": "xxx",
"version": "1.0.0",
"description": "xxx",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
"repository": "xxx",
"author": "xxx",
"license": "MIT",
"private": true,
"scripts": {
"build": "tsc",
"prepare": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
所以我已经看到的是我说我的主要是 dist/index.js 类型是 dist/index.d.ts 而我没有索引文件
所以我有多个问题
- src 中是否需要 index.ts?如果有,里面会有什么?
- 是否需要将类型更改为其他类型?
我基本上想为我创建的每个类输入类型,然后在另一个库中使用它,例如
import {UserClient} from "myLib"
import {BooksClient} from "myLib"
但是,当我现在使用 tsc 构建我的库时,它不会创建 index.d.ts(因为我没有 index.ts),而是直接创建 user-client.js 和 user-client.d.ts在 dist 文件夹中(不考虑现有结构),我也不能在我的其他库中使用它们
【问题讨论】:
标签: javascript typescript typescript-typings