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
Do the TS type definitions in Chart.js 3.0 support code written old-school web way, without modules, with the library included in HTML using <script>?
I have a file that uses Chart.js this way, written in TypeScript, and it worked fine with Chart.js 2.x, with the types from DefinitelyTyped. I'm trying to upgrade to 3.0 now, and the TypeScript compiler doesn't seem to recognize the name Chart. I can see it's going through the type files (npx tsc --traceResolution), but in the end it somehow doesn't make this declaration available to my script (error TS2304: Cannot find name 'Chart'.).
I've tried some different approaches - copying the .d.ts files to a directory inside my project, adding chart.js to node_modules using npm install, and adding some /// <reference ... /> directives at the top of my file (I didn't need those before with 2.x), but nothing seems to work.
I suspect this may be because (as I understand?) the objects are now exported there using the CommonJS way, but I'm not actually importing any modules in my script, since it relies on the Chart object being imported into window earlier. But I have a pretty poor understanding of how JS modules work in general, so I'm not sure.
Based on some similar issues I've found elsewhere, if I understand correctly, a possible workaround might be to write some kind of wrapper script that imports the types as a module and makes them available to the other script for the purposes of the TypeScript compiler, but I don't know enough about modules to figure this out. I've only ever used JS libraries by loading them with <script> and then accessing them through window.
Any ideas? Am I doing something wrong, or does Chart.js not support this use case (no modules, global context + TypeScript) in this version?
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
Uh oh!
There was an error while loading. Please reload this page.
-
Do the TS type definitions in Chart.js 3.0 support code written old-school web way, without modules, with the library included in HTML using
<script>
?I have a file that uses Chart.js this way, written in TypeScript, and it worked fine with Chart.js 2.x, with the types from DefinitelyTyped. I'm trying to upgrade to 3.0 now, and the TypeScript compiler doesn't seem to recognize the name
Chart
. I can see it's going through the type files (npx tsc --traceResolution
), but in the end it somehow doesn't make this declaration available to my script (error TS2304: Cannot find name 'Chart'.
).I've tried some different approaches - copying the
.d.ts
files to a directory inside my project, adding chart.js tonode_modules
usingnpm install
, and adding some/// <reference ... />
directives at the top of my file (I didn't need those before with 2.x), but nothing seems to work.I suspect this may be because (as I understand?) the objects are now exported there using the CommonJS way, but I'm not actually importing any modules in my script, since it relies on the
Chart
object being imported intowindow
earlier. But I have a pretty poor understanding of how JS modules work in general, so I'm not sure.Based on some similar issues I've found elsewhere, if I understand correctly, a possible workaround might be to write some kind of wrapper script that imports the types as a module and makes them available to the other script for the purposes of the TypeScript compiler, but I don't know enough about modules to figure this out. I've only ever used JS libraries by loading them with
<script>
and then accessing them throughwindow
.Any ideas? Am I doing something wrong, or does Chart.js not support this use case (no modules, global context + TypeScript) in this version?
Beta Was this translation helpful? Give feedback.
All reactions