简体   繁体   English

如何在 yarn package 中创建 index.d.ts 文件?

[英]How to create index.d.ts file in yarn package?

I have created yarn package, but I can't create index.d.ts file.我创建了 yarn package,但我无法创建index.d.ts文件。

This is my tsconfig.json :这是我的tsconfig.json

{
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"],
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "declaration": true,
    "outDir": "dist",
    "strict": true,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["es6", "es2020"],
    "baseUrl": ".",
    "types": ["node"],
    "paths": {
      "assets/*": ["./src/assets/*"],
      "components/*": ["./src/components/*"],
      "services/*": ["./src/services/*"],
      "types/*": ["./src/types/*"],
      "utils/*": ["./src/utils/*"]
    },
    "typeRoots": ["./node_modules/@types/"],
    "skipLibCheck": true
  }
}

This is package.json :这是package.json

{
  "name": "name",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "files": [
    "dist/**/*"
  ],
  "scripts": {
    "clean": "rm -rf ./dist",
    "build": "yarn run clean && tsc --project tsconfig.json && tscpaths -p tsconfig.json -s ./src -o ./dist",
    "publicate": "yarn version && yarn run build && yarn publish --access public"
  },
  "author": "author",
  "license": "MIT",
  "private": true,
  "devDependencies": {
    "@react-native-community/datetimepicker": "^3.5.2",
    "@react-navigation/native": "^6.0.2",
    "@types/react-native": "^0.64.13",
    "@types/react-native-htmlview": "^0.12.3",
    "@types/react-native-vector-icons": "^6.4.8",
    "axios": "^0.21.1",
    "formik": "^2.2.9",
    "react": "^17.0.2",
    "react-native": "^0.65.1",
    "react-native-barcode-mask": "^1.2.4",
    "react-native-document-picker": "^6.0.4",
    "react-native-dropdownalert": "^4.3.0",
    "react-native-fast-image": "^8.3.7",
    "react-native-floating-label-input": "^1.3.11",
    "react-native-heroicons": "^2.0.1",
    "react-native-htmlview": "^0.16.0",
    "react-native-image-picker": "^4.0.6",
    "react-native-location": "^2.5.0",
    "react-native-masked-text": "^1.13.0",
    "react-native-paper": "^4.9.2",
    "react-native-permissions": "^3.0.5",
    "react-native-size-matters": "^0.4.0",
    "react-native-svg": "^12.1.1",
    "react-native-tab-view": "^3.1.1",
    "react-native-vector-icons": "^8.1.0",
    "rn-fetch-blob": "^0.12.0",
    "tscpaths": "^0.0.9",
    "typescript": "^4.3.5"
  }
}

**.d.ts files only appear in dist subfolders. **.d.ts文件只出现在 dist 子文件夹中。 For example, I have component 'Plug' in path components/app/Plug/Plug.tsx .例如,我在路径components/app/Plug/Plug.tsx中有组件“Plug”。 After yarn build two files appear - Plug.js and Plug.d.ts .yarn build之后出现两个文件 - Plug.jsPlug.d.ts But file index.d.ts doesn't appear in dist folder.但是文件index.d.ts没有出现在 dist 文件夹中。

Am I doing something wrong?难道我做错了什么? How can I fix it?我该如何解决?

TypeScript does not know that you want to generate an index.d.ts , because there is no index.ts file that it can compile. TypeScript 不知道你要生成一个index.d.ts ,因为没有它可以编译的index.ts文件。

A common way to let users of your library import directly from a package is by having an index.ts file that exports all your modules.让库的用户直接从 package 导入的一种常见方法是使用一个导出所有模块的index.ts文件。 Something like this:是这样的:

export * from './moduleA';
export * from './moduleB';

While compiling, TypeScript generates a index.d.ts file with all the types that are in moduleA and moduleB .编译时, TypeScript 生成一个index.d.ts文件,其中包含moduleAmoduleB中的所有类型。

Right now, there is no fancy way to generate automatically an index.d.ts file in TypeScript.现在,没有什么奇特的方法可以在 TypeScript 中自动生成一个index.d.ts文件。

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

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