javascript - 发布依赖于 3rd 方 npm 包的 Typescript 包
问题描述
我正在使用 typescript v2.8 构建一个简单的 Typescript 包/库,该包/库是使用tsconfig的这个变体编译的(我尝试了各种配置但没有成功):
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom"],
"module": "umd",
"moduleResolution": "node",
"declaration": true,
"declarationDir": "dist/types",
"outDir": "dist/lib",
"typeRoots": [
"node_modules/@types",
],
}
}
和一个如下所示的package.json:
{
"name": "FooBarLib",
"version": "0.0.0",
"description": "A simple library",
"main": "dist/lib/index.js",
"typings": "dist/types/index.d.ts",
"author": "Gabriel C. Troia",
"license": "MIT",
"dependencies": {
"@types/ramda": "^0.25.35",
"@types/react": "^16.4.7",
"@types/react-dom": "^16.0.6",
"ramda": "^0.25.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"styled-components": "^3.3.3"
},
"devDependencies": {
"typescript": "2.8"
},
"peerDependencies": {
"ramda": "^0.25.35",
"react": "^16.4.1",
"styled-components": "^3.3.3"
},
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc"
}
}
正如您在上面看到的,我的 FooBarLib 库依赖于一堆外部库,例如react
和ramda
. 一切都在本地编译,我能够生成 js 转译文件以及类型定义文件。
但是,当我尝试将它导入一个单独的打字稿项目(使用打字稿 v2.9.2)时,编译器找不到我的 FooBarLib 中使用的任何第 3 方库类型(react、ramda 等),即使它们是安装在项目的 node_modules 中,因此失败并出现以下错误之一:
Cannot find module 'react'
或Cannot find module 'ramda'
,等等...
d.ts 生成文件的示例:
/// <reference types="ramda" />
import * as R from 'ramda';
export declare const myCurriedFunction: R.CurriedFunction2<string | undefined, string | string[] | undefined, string>;
此外,是否存在似乎并不重要/// <reference types="ramda" />
。发生同样的错误。
此外,导入 FooBarLib 的项目已@types/ramda
安装在本地。
这令人沮丧,除了这个项目https://github.com/alexjoverm/typescript-library-starter之外,我似乎找不到太多关于此的信息,但仅使用 tsconfig 或 package.json 并没有这样做。
更新
我发现通过在项目实现中手动导入第 3 方依赖项可以成功编译。
import { foo } from 'FooBarLib';
// 3rd party dependencies
import { StyledComponentClass } from 'styled-components';
import * as React from 'react';
import * as R from 'ramda';
foo();
此解决方案的问题在于,除了无缘无故地变得更加冗长之外,如果noUnusedLocals
设置为 true,它也会失败。
解决方案
推荐阅读
- python - 为什么通过argparse读取数据时我的json字符串数据丢失如$match,$group
- python - 从while循环中间循环的方式
- scala - 使用 Scala 从 List[Row] 中获取 Json Key 值
- java - 我应该如何实施 SSL?
- ruby-on-rails - 在更新记录栏时添加关联记录
- java - Springboot - 如何使用 manifest.yml 从本地类路径访问 cacerts 文件
- javascript - 提取对象数组中未知键的值
- java - 黄瓜自动测试无法通过詹金斯找到元素
- python - Openpyxl 观察工作表的 max_row?
- sql-server - 是否可以遍历 tsql 中的数字列表?