首页 > 解决方案 > 定义文件的打字稿输出文件错误

问题描述

我正在尝试使用 ts > js 文件和 ts 定义文件构建我的项目,并在编译时对 js 文件进行签名。

目前文件正在正确构建,但 .d.ts 文件被放入dist/src文件夹中,然后使用正确的路径。我怎样才能让这些在他们的 js 父母旁边编译。例如,dist/components/Select/index.d.ts而不是错误的方式:dist/src/components/Select/index.d.ts

包.json

"build": "rm -rf dist && npx tsc && NODE_ENV=production babel src/components --out-dir dist --extensions '.tsx' --presets @babel/preset-typescript --presets @babel/preset-env --ignore __tests__,__snapshots__,__mocks__,stories.js,**/*.test.tsx",

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "rootDirs": [
      "src",
      "stories",
      "config"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "jsx": "react-jsx",
    // Ensure that .d.ts files are created by tsc, but not .js files
    "declaration": true,
    "emitDeclarationOnly": true,
    // Ensure that Babel can safely transpile files in the TypeScript project
    "isolatedModules": true,
    "outDir": "./dist"
  },
  "include": ["./src/**/*"],
  "exclude": [
    "node_modules",
    "dist",
    "docs"
  ]
}

构建文件

dist
dist/FormGenerator
dist/Input
dist/Link
dist/List
dist/MoneyFormat
dist/Select
dist/Select/index.js
dist/Select/Select.js
dist/src
dist/src/components
dist/src/components/FormGenerator
dist/src/components/Input
dist/src/components/Link
dist/src/components/List
dist/src/components/MoneyFormat
dist/src/components/Select
dist/src/components/Select/index.d.ts
dist/src/components/Select/Select.d.ts
dist/src/components/Svg
dist/src/components/TemplateComponent
dist/src/components/Text
dist/src/components/index.d.ts
dist/src/themes
dist/src/setupTests.d.ts
dist/Svg
dist/TemplateComponent
dist/test-utils
dist/Text
dist/index.js

根目录结构

.github
.husky
.storybook
.vscode
dist
node_modules
reports
src
test-utils
.codacy.yml
.editorconfig
.env
.env.local
.eslintignore
.eslintrc
.gitignore
.npmrc
.prettierignore
.prettierrc
jest.config.js
package.json
README.md
setupFile.js
tsconfig.json
yarn.lock

标签: javascripttypescript

解决方案


您在 src 目录之外有任何 .ts 文件吗?如果是这样,那么编译器也会编译该文件,并且最终的文件夹结构将包含 src。


推荐阅读