[WP] 24.02.2023 | Typescript intro

fig 1.1 example tsconfig contents

----------------------------------------------------------------------

 To create typescript project type: npx tsc –init. It basically creates tsconfig.json file within your project folder. Basic tsconfig.json file contents will be as follows:

There are some settings such as no ts lint inside eslintrc.json config file. Unary + is an trick done in javascript. if we add + to single string it will be coerced into a number. Good example of using TS is to type API responses. With well-defined API response types TS will make sure that the form of the reponses recieved from the API will be always in the same form or TS will throw an error. When people with C languages use interfaces they tend to use capital I before the name of the interface – interface Isomething {…} .

To make repetition of TS i reviewed an exercise of migration of app from JS to TS from Frontend Masters Intro to React Course. It was about adjusting simple pet-adoption react SPA from .js & .jsx files to .ts & .tsx files. When it comes to Typescript… There are some specific types such as ReactElementMutableRefObject – they were imported from external libraries. „caret parenthesses” < > are used for type parameters to build generic types. On example for MutableRefObject<HTMLDivElement || null> is a parameter to mutable ref object to tell it how to structure itself.

There were three descriptions to APIresponse – one type and two interfaces. The type was an example of literal type. type Animal = „dog”|”cat”|”bird… – it is an example of literal type. It means that every variable with Animal type is bound to hold one of the values literally specified within the TS type definition. Interfaces specify type for complex data structures(objects) and specify how many fields the object need to have, names of the fields and what is specific type of each and every field within object complying with created interface. After definintion type and interfaces were exported with ES6 export syntax. This allows us to reuse these response types anywhere within an app.

TS requires user to be explicit about types whenever it cannot infer default types. With strict linting settings TS will not accept any as a type. TS forces a lot of defensive programming.

Backlog no 2

gmarcin

Komentarze

Popularne posty z tego bloga

[WP] 23.06.2023 | subjects

15.07.2024 | simplicity

[WP] 05.06.2023 | angular-intro