-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not rely on
any
to extend the option context
This is a breaking change for TypeScript projects since now, in order to extend the option context, it's required to declare a custom type or to use module augmentation. Add TypeScript documentation but also a migration guide for the upcoming versions 1.x & 2.x.
- Loading branch information
1 parent
e8fa4f3
commit b5cd8aa
Showing
9 changed files
with
158 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Migration | ||
|
||
## Migrating to v1.0.0 | ||
|
||
### Breaking Changes | ||
|
||
#### Extending the Option Context <Badge text="TS only"/> | ||
|
||
In order to extend the [option context](options.md#option-context), you now need to use one of the methods described [in this section](typescript.md#option-context). Peviously, this feature relied on the use of `any`. If for whatever reasons you need that flexibility, the old behavior can be achieved using: | ||
|
||
```ts | ||
import {Context} from 'chartjs-plugin-datalabels'; | ||
|
||
// OLD BEHAVIOR: NOT RECOMMENDED! | ||
declare module 'chartjs-plugin-datalabels' { | ||
interface Context { | ||
[key: string]: any; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# TypeScript | ||
|
||
This plugin provides TypeScript type declaration files bundled in the npm package so that types of this plugin options are checked when building your TypeScript project. Note that you also need the `@types/chart.js` package, if not already installed: | ||
|
||
```sh | ||
npm install --save-dev @types/chart.js | ||
``` | ||
|
||
## Option Context | ||
|
||
The declaration of the [option context](options.md#option-context) is exported as `Context`: | ||
|
||
```ts | ||
import {Context} from 'chartjs-plugin-datalabels'; | ||
|
||
const chart = new Chart('foo', { | ||
options: { | ||
plugins: { | ||
datalabels: { | ||
rotation: (ctx: Context) => { | ||
return ctx.dataIndex % 2 ? 180 : 0; | ||
}, | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
|
||
Extending this context can be done using one of the following methods: | ||
|
||
### Custom Interface | ||
|
||
```ts | ||
import {Context} from 'chartjs-plugin-datalabels'; | ||
|
||
interface FooContext extends Context { | ||
foo?: number; | ||
} | ||
|
||
const chart = new Chart('foo', { | ||
options: { | ||
plugins: { | ||
datalabels: { | ||
rotation: (ctx: FooContext) => ctx.foo || 0, | ||
listeners: { | ||
click: (ctx: FooContext) => { | ||
ctx.foo += (ctx.foo || 0) + 10; | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
|
||
::: tip | ||
This method allows to declare different context interfaces to use in different charts. | ||
::: | ||
|
||
### Module Augmentation | ||
|
||
```ts | ||
// shims-chartjs-plugin-datalabels.d.ts | ||
import {Context} from 'chartjs-plugin-datalabels'; | ||
|
||
declare module 'chartjs-plugin-datalabels' { | ||
interface Context { | ||
foo?: number; | ||
} | ||
} | ||
``` | ||
|
||
```ts | ||
// index.ts | ||
const chart = new Chart('foo', { | ||
options: { | ||
plugins: { | ||
datalabels: { | ||
rotation: (ctx: Context) => ctx.foo || 0, | ||
listeners: { | ||
click: (ctx: Context) => { | ||
ctx.foo += (ctx.foo || 0) + 10; | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
::: warning | ||
The augmented `Context` declaration will be the same for all charts. This method should be considered only if all charts should share the same context declaration. Read more about [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation). | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Chart} from 'chart.js'; | ||
import {Context} from '../context'; | ||
import {Options} from '../options'; | ||
|
||
interface CustomContext extends Context { | ||
foo?: number; | ||
} | ||
|
||
const options: Options = { | ||
rotation: (ctx: CustomContext) => ctx.foo || 0, | ||
listeners: { | ||
click: (ctx: CustomContext) => { | ||
ctx.foo = 42; | ||
} | ||
}, | ||
}; | ||
|
||
const chart = new Chart('id', { | ||
data: { | ||
datasets: [ | ||
{ | ||
datalabels: options | ||
} | ||
] | ||
}, | ||
options: { | ||
plugins: { | ||
datalabels: options | ||
} | ||
} | ||
}); |