2

I have troubles using Amplitude with React and Typescript. Is anyone else using this?

How do you import Amplitude and initialise correctly?

When using

import amp from 'amplitude-js';

I get a type error about a missing default export.

TS7016: Could not find a declaration file for module 'amplitude-js'. '[shortened]/node_modules/amplitude-js/amplitude.js' implicitly has an 'any' type.

Thank you.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 2
    Some modules are written only in javascript and not typescript, so they don't have any types and typescript complains that it doesn't know what to do with these. In such cases you need to install types manually: `npm install --save-dev @types/amplitude-js` – Alex Chashin Nov 19 '20 at 21:33

1 Answers1

4

Historically Amplitude hasn't offered typescript typings with the SDK. There are 3rd party once out there, for example, https://www.npmjs.com/package/@types/amplitude-js where I've contributed.

You could maybe also use our Nodejs SDK which has typescript instructions here https://developers.amplitude.com/docs/nodejs but its build mainly as a server-side package or to be used tracking electron apps for example.

To answer the exact question, I think

import { AmplitudeClient } from 'amplitude-js'; // "@types/amplitude-js": "4.4.5",

Should work. Note version might be incorrect as that is from a piece of code written February this year.

Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63
  • `import amplitude from "amplitude-js"; amplitude.getClient()` works on latest v8 @types/amplitude-js and amplitude-js – Salami Nov 02 '21 at 20:24
  • Amplitude has since released Amplitude-Typescript https://github.com/amplitude/Amplitude-TypeScript – ddblair Feb 21 '23 at 22:04