2

My code works but under my import statements I have 2 dots and if I hover over them it shows the error

Could not find a declaration file for module 'chartjs-adapter-date-fns'. '/home/user/dev/website/client/node_modules/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/chartjs-adapter-date-fns` if it exists or add a new declaration (.d.ts) file containing `declare module 'chartjs-adapter-date-fns';`ts(7016)

I tried yarn add -D @types/chartjs-adapter-date-fns but that seems not to exist. Is the problem that I am missing the typescript types? How can I get rid of this error?


My import statements just in case:

import {
  CategoryScale,
  Chart as ChartJS,
  Filler,
  Legend,
  LinearScale,
  LineElement,
  PointElement,
  TimeScale,
  Title,
  Tooltip,
} from 'chart.js'
import 'chartjs-adapter-date-fns'
import { enGB } from 'date-fns/locale'
import { Line } from 'react-chartjs-2'
import { useMarketDataQuery } from '../generated/graphql'
import BaseCard from './BaseCard'

2 Answers 2

2

You need to specify your own type definitions for this package.

There are three ways how the library can be typed:

  • Bundled Types
  • DefinitelyTyped / @types
  • Your Own Definitions

As the types aren't bundled with the chartjs-adapter-date-fns package and there are no @types for it in the DefinitelyTyped repository, the only option you have is to provide your own type definition for this package.

The easiest way to do it is to add an empty declaration for it in a .d.ts file in your project with the content.

declare module "chartjs-adapter-date-fns";

It will silence warnings about this module

Sign up to request clarification or add additional context in comments.

2 Comments

Where would you put this file? should I make a folder src/types?
Yes, it might be a good option. It's up to you where to put declaration files. It's important to make sure that these files are included in tsconfig.json
1

The date adapter does not export any functionalities so you can just add a // @ts-ignore above the import or create your own declaration file whith an empty module that provides the typing for the date adapter.

All the typings for the config itself are already provided by chart.js itself.

4 Comments

I tried the // @ts-ignore method but that didn't work - any idea why that could be?
No also cant seem to reproduce it, dont get any errors on it: typescriptlang.org/play?#code/…
Could the issue with not being able to reproduce it be from my tsconfig? I have { "compilerOptions": { "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true },
Don't know enough about that specifically to give you an answer about that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.