How To Use Typescript With React and Redux - by Ross Bulat - Medium
How To Use Typescript With React and Redux - by Ross Bulat - Medium
This is your last free story this month. Sign up and get an extra one for free.
1 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Create React App now has Typescript support built into the
package, since react-scripts 2.1 . It can simply be included in a
medium.com
2 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
#or
.js files are now .tsx files. The Typescript compiler will pick
up all .tsx files at compile time.
Running yarn start at this stage will compile and run your app,
yielding the identical bootstrapped app in your browser to the
original Javascript-only Create React App counterpart.
3 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Installing TSLint-React
Installing linting tools are extremely helpful with Typescript and
React; you will find yourself referring to tooltips to obtain a certain
type, especially with events. The linting tools are extremely strict
with their default setup, so we will omit some of these rules in the
installation procedure.
Note: These tools are installed via NPM or Yarn and are not tied to a
specific IDE. We will firstly install these tools before the specific
packages for Sublime Text 3.
tslint --init
{
"defaultSeverity": "error",
"extends": [
"tslint-react"
],
4 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
"jsRules": {
},
"rules": {
"member-access": false,
"ordered-imports": false,
"quotemark": false,
"no-console": false,
"semicolon": false,
"jsx-no-lambda": false
},
"rulesDirectory": [
],
"linterOptions": {
"exclude": [
"config/**/*.js",
"node_modules/**/*.ts"
]
}
}
5 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Note: You may receive an error to say that the tslint module was not
found upon restarting Sublime Text. If this was the case, re-install the
packages locally rather than globally, that we opted for (and what
documentation suggests) previously:
VS Code Extension
For the Visual Studio Code editor, install the TSLint extension for
full Typescript support.
6 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
components.
DeJining interfaces
Applying an interface to components will force us to adhere to
such data structures when passing props into a component,
ensuring that they are all accounted for while also stopping
unwanted props to be passed down.
interface FormProps {
first_name: string;
last_name: string;
age: number;
agreetoterms?: boolean;
}
interface FormState {
submitted?: boolean;
full_name: string;
age: number;
}
Note: Tslint used to prompt us to use a capital “i” in front of all our
interface names, e.g. IFormProps and IFormState would be the above
names. However, it is no longer enforced by default.
7 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Note: In the event you have no props but would like to define state,
you can place either {} or object in place of FormProps . Both values
Importing interfaces
Defining groups of interface definitions in one file is good practice;
a common convention is to create a src/types/ folder with groups
of your interfaces:
// src/types/index.tsx
8 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
// src/components/MyForm.tsx
// define enum
enum HeardFrom {
SEARCH_ENGINE = "Search Engine",
FRIEND = "Friend",
OTHER = "Other"
}
9 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Typing Events
In the event (no pun intended) you wish to type events, such as
onChange or onClick events, utilise your syntax tools to obtain the
exact event you need.
React.ChangeEvent<HTMLInputElement>:
10 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
I have also typed the name and value objects of e with the
following syntax:
11 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
medium.com
// src/types/index.tsx
/index.tsx file:
// src/constants/index.tsx
12 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
These const & type objects can now be imported into your
src/actions/index.tsx file, where we can define action interfaces
and the actions themselves, and typing them along the way:
// src/actions/index.tsx
//define actions
export function setLanguage(l: string): SetLanguage ({
type: constants.SET_LANGUAGE,
language: l
});
13 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
case as Authenticate .
Back in our actions file, add a union type for locality under our
interfaces:
// src/actions/index.tsx
14 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Now we can apply this Locality type to our locality reducer action,
in bold text below:
// src/reducers/index.tsx
switch (action.type) {
case SET_LANGUAGE:
return return { ...state, language: action.language};
case SET_COUNTRY:
return { ...state, country: action.country};
case AUTHENTICATE:
return {
...state,
auth: {
username: action.username,
authenticated: true
}
};
}
return state;
}
15 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Our constant & type pairs are being utilised here too, as a means
to switch between actions.
// src/index.tsx
16 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
// mapStateToProps example
interface LocalityProps = {
country: string;
language: string;
}
// mapDispatchToProps example
const mapDispatchToProps = {
actions.setLanguage,
actions.setCountry
}
required to wrap our actions within dispatch() . We can also emit the
17 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
. . .
From here, you are now well placed to study the full feature set of
Typescript in the official documentation.
18 of 19 23/8/20, 3:13 am
How to Use Typescript with React and Redux | by Ross Bulat... https://medium.com/@rossbulat/how-to-use-typescript-with-r...
Welcome to a place where Follow all the topics you care Get unlimited access to the
words matter. On Medium, about, and we’ll deliver the best stories on Medium —
smart voices and original best stories for you to your and support writers while
ideas take center stage - homepage and inbox. Explore you’re at it. Just $5/month.
with no ads in sight. Watch Upgrade
19 of 19 23/8/20, 3:13 am