随手记录d.ts文件打包问题

AI 概述
一、全局命名空间使用声明使用二、 解决打包后命名空间失效问题问题一:打包后 d.ts 文件丢失问题二:d.ts 文件打包输出后,全局命名未生效 在 TS 项目中,有太多一样的类型声明,重复声明显然是最不明智的选择。还有一种方式就是采用 export,import 方式。但是每个使用的文件都需要 imort,或者一个...
目录
文章目录隐藏
  1. 一、全局命名空间使用
  2. 二、 解决打包后命名空间失效问题

在 TS 项目中,有太多一样的类型声明,重复声明显然是最不明智的选择。还有一种方式就是采用 export,import 方式。但是每个使用的文件都需要 imort,或者一个文件需要 import 多个类型声明,都会显得代码冗杂。所以我们可以在项目中采用在 d.ts 文件中声明全局声明命名空间的方式。

但是在组件库项目中,就会遇到打包后找不到命名空间的问题。

一、全局命名空间使用

声明

declare namespace Feature {
  export type BreadcrumbItem = {
    id: string;
    label: string;
  }
}

使用

type IProps = {
  data: Feature.BreadcrumbItem[];
}
const Breadcrumb: React.FC<IProps> = (props) => {
    return <div>{props.id}</div>
}

二、 解决打包后命名空间失效问题

问题一:打包后 d.ts 文件丢失

原因:.d.ts文件被视为编译器进行类型检查的不可修改的输入。它们不用于任何输出生成,这也意味着它们不会被复制到build

The .d.ts files you use are an input to the build system but not an output. It’s perfectly reasonable to consume some types from a .d.ts but have your output not use those types, so there’d be no reason to distribute the input .d.ts with your build results. […] It sounds like you’ll want a post-build step in your build tool to copy the relevant .d.ts files wherever you need them.The .d.ts files are considered “references” the compiler will not touch them, not move them, or recreate them. An easy way to think of the .d.ts files is that they go along with your .js files. if you are copying the .js files, you should copy the matching .d.ts.

解决:通过手动构建步骤复制 d.ts 文件。

我构建工具用的是 gulp,其他构建工具操作类似:

gulp.task('copy', done => {
    return gulp.src('src/typings/*')//需要被复制的文件夹
      .pipe(gulp.dest('lib/typings'));//复制后输出位置
});

问题二:d.ts 文件打包输出后,全局命名未生效

原因一:打包后的 d.ts 文件未编译

解决:修改 tsconfig.json 文件

{
  "compilerOptions": {
    "module": "commonjs",
    "lib": [
      "es6"
    ],
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "baseUrl": "../",
    "typeRoots": [
      "../"
    ],
    "types": [],
    "noEmit": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "./lib" //需要 ts 编译的文件夹
  ],
}

原因二:打包后的 d.ts 文件未引入

1. package.json 中的 types 或 typings 字段指定一个类型声明文件地址。比如:

{
    "name": "foo",
    "version": "1.0.0",
    "main": "lib/index.js",
    "types": "index.d.ts",
}

2. 打包前,在指定的类型声明文件中采用三斜线指令引入命名文件 feature.d.ts

/// <reference types="./typings/feature" />

随手纪录,希望能帮到正在被此问题困扰的你。

以上关于随手记录d.ts文件打包问题的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » 随手记录d.ts文件打包问题

发表回复