可以从另一个包中增加一个包的输入吗?

时间:2017-11-10 03:18:46

标签: typescript

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

npm install chart.js chart.piecelabel.js

我正在从DefinitelyTyped:

安装Chart.js的类型
npm install @types/chart.js

我想为Chart.PieceLabel.js repo贡献一些打字,但我无法让我的应用程序识别它们(它记录了Chart.js的输入,但是当我npm link或编辑时类型直接在node_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={{..}} />

我的提示是在此提交中:that single thread

我的应用代码在这里:https://github.com/bcherny/Chart.PieceLabel.js/commit/9e6744c

一个孤立的复制案例在这里https://git.io/vF23g,并且工作正常。

我的tsconfig.json配置错误了吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

要使扩充生效,定义扩充的模块必须包含在编译中。

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

@types, typeRoots and types

  

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

但是当它包含在node_modules的主要javascript包中时,必须在某处明确引用它。您可以在.d.ts的{​​{1}}部分明确地添加files,或者只需执行tsconfig.json而无需导入任何内容,它会被称为&#34; {{ 3}}&#34;

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

import

将您的更改应用于npm i chart.js chart.piecelabel.js @types/chart.js react-chartjs-2 @types/react

并尝试使用typescript 2.6 - 它编译:

node_modules/chart.piecelabel.js

<强>更新

这是我的步骤:

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
});

它说&#34;版本2.6.1&#34;

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

开始吐出很多错误
./node_modules/.bin/tsc

当我从 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. 中删除"strict": true时,剩下的唯一错误是:

tsconfig.json

我认为第一个是由import for side-effects only引起的,我不知道其他的,但我在Donut选项中没有看到与src/services/api.ts(12,11): error TS2345: Argument of type '{ body: string; headers: { Accept: string; 'Content-Type': string; }; method: "GET" | "POST"; }' is not assignable to parameter of type 'RequestInit'. Types of property 'headers' are incompatible. Type '{ Accept: string; 'Content-Type': string; }' is not assignable to type 'Headers | string[][]'. Object literal may only specify known properties, and 'Accept' does not exist in type 'Headers | string[][]'. src/services/effects.ts(80,17): error TS2339: Property 'distance_to_closest_provider' does not exist on type '{}'. src/services/effects.ts(81,17): error TS2339: Property 'time_to_closest_provider' does not exist on type '{}'. src/services/effects.ts(85,19): error TS2339: Property 'id' does not exist on type '{}'. src/services/effects.ts(86,42): error TS2339: Property 'distance_to_closest_provider' does not exist on type '{}'. src/services/effects.ts(87,38): error TS2339: Property 'time_to_closest_provider' does not exist on type '{}'. src/services/effects.ts(88,42): error TS2339: Property 'closest_provider_by_distance' does not exist on type '{}'. src/services/effects.ts(89,38): error TS2339: Property 'closest_provider_by_time' does not exist on type '{}'. 相关的任何错误。

更新2

我刚试过不同版本的打字稿:

pieceLabel

没有错误

然而webpack说:

npm i typescript@2.5
./node_modules/.bin/webpack

所以也许你可能会在Failed to load ./.env. 文件中有一些影响这个的东西。

另外 - 我尝试过打字稿2.4 - 没有错误。