Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 可以从另一个包中增加一个包的类型吗?_Typescript - Fatal编程技术网

Typescript 可以从另一个包中增加一个包的类型吗?

Typescript 可以从另一个包中增加一个包的类型吗?,typescript,Typescript,我的应用程序正在使用Chart.PieceLabel.js,它增强了Chart.js: npm install chart.js chart.piecelabel.js 我正在从DefiniteTyped安装Chart.js的类型: npm install @types/chart.js 我想为Chart.PieceLabel.js repo贡献打字,但我无法让我的应用程序识别它们(它为Chart.js注册打字,但当我npm link或直接在node\u modules/Chart.Piec

我的应用程序正在使用Chart.PieceLabel.js,它增强了Chart.js:

npm install chart.js chart.piecelabel.js
我正在从DefiniteTyped安装Chart.js的类型:

npm install @types/chart.js
我想为Chart.PieceLabel.js repo贡献打字,但我无法让我的应用程序识别它们(它为Chart.js注册打字,但当我
npm link
或直接在node\u modules/Chart.PieceLabel.js中编辑类型时,TypeScript看不到我的增强

我在我的应用程序中使用Chart.js和Chart.PieceLabel.js,如下所示:

import { Chart } from 'chart.js'
import 'chart.piecelabel.js'
import { Doughnut } from 'react-chartjs-2'

let chart = <Doughnut options={{..}} />
从'Chart.js'导入{Chart}
导入“chart.piecelabel.js”
从'react-chartjs-2'导入{Doughnut}
让图表=
我的打字在以下位置:

我的应用程序代码在这里:

这里有一个单独的复制案例,效果很好

我的tsconfig.json是否配置错误


谢谢!

为了使增强生效,定义增强的模块必须包含在编译中

如果您将
Chart.PieceLabel.d.ts
作为@types中的单独包分发,则它将自动包含:

默认情况下,所有可见的“@types”包都包含在您的 编译。任何封闭文件夹的node_modules/@类型中的包 被认为是可见的;具体来说,这意味着 ./node_modules/@types/,../node_modules/@types/, ../../node_modules/@types/等

但是当它包含在
节点_modules
的主javascript包中时,必须在某个地方显式引用它。您可以在
tsconfig.json
文件
部分显式包含它的
d.ts
,或者您可以只执行
导入
,而不导入任何内容,它被称为“”

我只是试着安装所有必要的打字机

npm i chart.js chart.piecelabel.js @types/chart.js react-chartjs-2 @types/react
将您的更改应用于
节点\u模块/chart.piecelabel.js

并用typescript 2.6尝试了这一点-它编译:

import 'chart.piecelabel.js'

import * as Chart from 'chart.js';

import * as React from 'react';
import { Doughnut } from 'react-chartjs-2'

let o: Chart.ChartOptions = {
    responsive: true,
    pieceLabel: {
        render: 'label'
    }
};


let d = React.createElement(Doughnut, {
    options: {
        responsive: true,
        pieceLabel: {
            render: 'label'
        }
    },
    data: undefined as any // just to make it compile
});
更新

以下是我的步骤:

git clone https://github.com/bayesimpact/tds-frontend.git
cd tds-frontend
git checkout personal/bcherny/publish-types
npm i  
./node_modules/.bin/tsc -v
上面写着“版本2.6.1”

它从一开始就抛出了很多错误

 src/components/LeftPane/Drawers/ServiceAreasDrawer/
      ServiceAreasDrawer.tsx(64,10): error TS2684: The this context of type 
'LoDashExplicitWrapper<[string, string][]>' is not assignable to method's 'this' of type 'LoDashExplicitWrapper<ArrayLike<[string, string]> | Dictionary<[string, string]> | NumericDiction...'.
      Types of property 'push' are incompatible.
          Type '<T>(this: _.LoDashExplicitWrapper<ArrayLike<T> | null | undefined>, ...items: T[]) => _.LoDashExp...' is not assignable to type '<T>(this: _.LoDashExplicitWrapper<ArrayLike<T> | null | undefined>, ...items: T[]) => _.LoDashExp...'. Two different types with this name exist, but they are unrelated.
              Type 'LoDashExplicitWrapper<[string, string][]>' is not assignable to type 'LoDashExplicitWrapper<ArrayLike<[string, string]> | Dictionary<[string, string]> | NumericDiction...'. 
src/components/LeftPane/Drawers/ServiceAreasDrawer/ 
    ServiceAreasDrawer.tsx(65,10): error TS7006: Parameter '_' implicitly has an 'any' type.
我认为第一个原因是,我不知道其他原因,但我没有看到甜甜圈选项中与
pieceLabel
相关的任何错误

更新2

我刚刚尝试了不同版本的typescript:

npm i typescript@2.5
./node_modules/.bin/webpack
没有错误

但该网页称:

Failed to load ./.env.
因此,您可能在
.env
文件中有一些内容会影响这一点


另外-我尝试了typescript 2.4-也没有错误。

谢谢您的回复,但我不认为这是我遇到的问题。我用代码示例编辑了原始问题-这有帮助吗?我相信我的问题与没有使用正确的语法声明Chart.PieceLabel.js的类型有关。我更新了答案-我有编译ju的代码在安装了所有类型后,您的确切类型定义会很好。可能是JSX语法的问题,但我没有尝试,因为我自己再也不用了。谢谢您的帮助,但似乎还有其他问题-看起来这在我的应用程序中是个问题。一个单独的测试用例()可以正常工作,但在我的应用程序中()它仍然失败。我有点不知所措。奇怪。我刚刚克隆了,
npm I
,我在
node\u modules/chart.piecelabel.js
中没有看到您的类型,所以我复制了
chart.piecelabel.d.ts
,并将
类型
行添加到
package.json
,然后从
Adeq中的甜甜圈选项中删除
uacyDoughnut.tsx
和-它可以编译!我看到许多其他不相关的错误,但它似乎在
选项中识别
片段标签
。您看到了哪些不相关的错误?复制步骤是(1)克隆repo,(2)签出
个人/bcherny/发布类型
分支,(3)运行
npm I
,(4)运行
npm build
。按照这些步骤,我仍然看到一个错误!您也可以在这里的CI上看到它。
npm i typescript@2.5
./node_modules/.bin/webpack
Failed to load ./.env.