3

I am a beginner with react (even tho I did some ReactJs already) and I am today trying to create a simple app using TypeScript (instead of pure Javascript).

However, when running npm start, I found an issue that I understand, but I can't understand why it is an issue.

Let say we have this code (main.tsx):

import * as React from 'react';

Can you explain to me clearly what is the difference with

import * as React from '@types/react';

In my package.json I have "@types/react": "^16.9.2" and I don't have react, so the issue is quite logic that the package couldn't be found. But then what's the difference between these two imports?

Also, I can't even import @types/react, I can only install it

Thanks !

1 Answer 1

3

The ones with @types prefix are type declaration packages, more info here.

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

3 Comments

Hey ! Thanks for the answer. Then I would have a second question: When do I know that I need the @types/ version of a package?
@Emixam23: If you don't get "intellisense/code completion" when using a library you can try searching for/installing the corresponding @types package. You can search using npm search @types/package_name or microsoft.github.io/TypeSearch. So you don't need the @types package, but it's helpful.
I'm kind of in the same boat sure old timers know the difference. Emixam23 gave a great answer, just want to add as Typescript is getting adopted more, some libraries are now including the types in the library for you. Couldn't find the types file for Axios, simply because they rock! They have already provided the types, no need for an extra types file. Just thinking this will be common for noobs like myself getting up to speed. Think the other tip given, autocomplete and seeing type related errors, you see them, you have all you need :)

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.