Amplitude with React and Typescript

Viewed 1587

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.

1 Answers

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.

Related