Using typescript with react.

Using typescript with react.

Here we will learn step by step process for starting the #typescript with a #reactjs .

Steps:

  1. create a typescript app.

  npx create-react-app your-app --template typescript 

        

you will see, the TSX file in the src folder. it's similar to the JSX but with extra type annotation.

No alt text provided for this image

Delete this src folder. we will write code from scratch for better understanding.

2. now make a new index.tsx file in the src folder and write the below code in that. here you notice that we do not write any code specific to typescript but still it's valid typescript code. and if you hover on any element then it will show types. this is called type inference means typescript auto guess type value given by their initial value.

No alt text provided for this image

DEFINE PROPS TYPE

if we pass props, as normal, then it will show the below error. we tell typescript to the interface of particular props.

No alt text provided for this image

to avoid this error, make a new interface for props and assign them as shown below.

No alt text provided for this image

DEFINE STATE TYPE

We can define property as we define in the normal javascript class. Let's make a simple counter for our app.

  1. here we have state object with count property. typescript set type count to number as we define initial value as a number.

No alt text provided for this image

2. But let's say we want to initialize the state in the constructor function. in javascript, their both are the same. but in typescript, it will give an error.

No alt text provided for this image

the state is inherited properly from React.component. and which default value is {}. so typescript didn't get count property value in state object (it's empty from typescript perspective.). to solve this pass second state interface as the second argument of generics in React.component

No alt text provided for this image

however if we define the state in the current App component as shown in the first example, then it will override the state variable. so in that case typescript got to state.count and not throw an error.

if we give any other property then count, typescript gives an error.

?constructor(props: AppProps) {

? ? super(props);

? ? this.state = { num: 0 }; ?// Type '{ num: number; }' is not assignable to type
                                 'Readonly<AppState>'

? }        

3. but what if use provide both methods, like property state in App component and also give AppState generics to React.component?

in this scenario, state property must have count property. apart from this, if we add new property then it will merge both types. like below we add num 0. so typescript have now both count and num value. and will not throw errors as above.

No alt text provided for this image

Use typescript in the functional components.

we can pass prop types as a normal function parameter types.

for defining the type of state, we can pass as generics to state function as shown below.

No alt text provided for this image

要查看或添加评论,请登录

Meet Vaghasiya的更多文章

  • START EXPRESS JS WITH TYPESCRIPT

    START EXPRESS JS WITH TYPESCRIPT

    In this article, we will learn How to install express js with typescript and minimal setup configuration of…

  • Redux with typescript

    Redux with typescript

    Let's see how we use redux with typescript install react typescript app npx create-react-app typescript-practise…

  • Index signature and keyof operator in Typescript

    Index signature and keyof operator in Typescript

    Index signature Let's say we have two types of objects. and you want to make a common function that returns total marks.

  • Use generics like pro

    Use generics like pro

    There is a rule in programming called DRY- don't repeat yourself. generics help us to use this convention in typescript.

  • High-level overview of type and interface in Typescript

    High-level overview of type and interface in Typescript

    Standard javascript object is a map of key: value pairs. When we define an object with properties (keys) and values…

  • Classes in typescript - 6thday of 100daysofcoding

    Classes in typescript - 6thday of 100daysofcoding

    Typescript is fully supported to work with modern javascript keyword class. Generally, classes are blueprints to make…

  • Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions: Functions are a very important part of any programming language as it provides reusability. let's see, how…

  • ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY IN TYPESCRIPT In typescript, typescript need to know type of each items in array. so it will tell us error if we…

  • TYPE- any in ts - DAY3 OF 100DAYSOFCODE

    TYPE- any in ts - DAY3 OF 100DAYSOFCODE

    Any type in typescript in the previous article, if you remember the last moment, if we don't declare and initialize…

    2 条评论
  • Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    What is a type system? When we learn any programming languages, first they introduces with various data-types of that…

社区洞察

其他会员也浏览了