You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is that Chart.js has this wrapped (I think) in a _DeepPartialObject<>, which means that foo and bar become optional against my intention. Can I somehow counteract this, or how should I deal with it? I would like all keys to be required. I feel like I should be able to utilize TypeScipt's Required<> somehow, but I can't seem to figure out how.
For more context, I then have something like this defined:
constmyChartOptions: ChartOptions<'bar'>={// other options,plugins: {myPlugin: {foo: 'something',bar: 'something else',},},};
Here TypeScript is fine with foo and/or bar missing. Then, when defining the action of the plugin, one thing I've tried looks something like:
constmyPlugin : Plugin<'bar'>={id: 'my-plugin',afterDraw: function(chart,_args,options: MyPluginOptions){if(options){constx=options.foo;consty=options.bar;// more code};};
Since I "cheated" by writing options: MyPluginOptions, TypeScript infers that foo and bar exist. But it also infers that options exists, which it could not, so that's can be a problem. Alternatively, I could have
constmyPlugin : Plugin<'bar'>={id: 'my-plugin',afterDraw: function(chart){constoptions=chart.plugins?.myPluginasMyPluginOptions;if(options){constx=options.foo;consty=options.bar;// more code};};
This mostly gives me what I want, but doesn't provide non-partial type checking when setting the options.
Other workarounds I can image include:
Setting the options in an unrelated object to circumvent the Chart.js typing entirely.
Actually treating them as optional keys and define default behavior on undefined keys. (Seems unnecessary in my use case, but I suspect this is the intention of the Chart.js team?).
I would love to get some more insight, especially if I'm missing the mark on the preferred way to do things.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
[Cross-posted and adapted from https://stackoverflow.com/q/74395708/8550319].
I'm using Chart.js with React and TypeScript. Following this answer, I've typed my custom plugin options in an
index.d.ts
-file, like so:The problem is that Chart.js has this wrapped (I think) in a
_DeepPartialObject<>
, which means thatfoo
andbar
become optional against my intention. Can I somehow counteract this, or how should I deal with it? I would like all keys to be required. I feel like I should be able to utilize TypeScipt'sRequired<>
somehow, but I can't seem to figure out how.For more context, I then have something like this defined:
Here TypeScript is fine with
foo
and/orbar
missing. Then, when defining the action of the plugin, one thing I've tried looks something like:Since I "cheated" by writing
options: MyPluginOptions
, TypeScript infers thatfoo
andbar
exist. But it also infers thatoptions
exists, which it could not, so that's can be a problem. Alternatively, I could haveThis mostly gives me what I want, but doesn't provide non-partial type checking when setting the options.
Other workarounds I can image include:
I would love to get some more insight, especially if I'm missing the mark on the preferred way to do things.
Beta Was this translation helpful? Give feedback.
All reactions