The Typescript Book
Notes on how to use the Typescript language
How to create a frontend development environment for Typescript
from node:alpine
workdir /usr/application
cmd ["npm", "start"]
copy package.json .
run npm install
copy . .version: '3'
services:
application:
stdin_open: true
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 3010:3000
volumes:
- .:/usr/application
- /usr/application/node_modules(1) create a directory called "frontend"
$ mkdir frontend && cd frontend
(2) create the react application in a directory
$ npx create-react-app . --typescript
(3) delete node_modules that create-react-app created in your local directory i.e.
$ rm -rf node_modules
(4) build an image based on your dockerfile.dev
$ docker-compose build
(5) start up a container based on your built image
$ docker-compose up
(6) go to http://localhost:3010 to see the deployed application
(7) make changes in your local directory and see them reflected in the browser
(8) shut down your container when you are done
$ docker-compose downHow to create a backend development environment for Typescript
FROM node:alpine
WORKDIR /usr/server
CMD ["npm", "run", "development"]
COPY package.json .
RUN npm install
COPY . .version: '3'
services:
server:
stdin_open: true
build:
context: .
dockerfile: dockerfile.dev
ports:
- 4010:4000
volumes:
- .:/usr/server
- /usr/server/node_modulesimport express from 'express'
const server: express.Application = express()
server.get('/', (request, response) => {
response.send('bye')
})
server.listen(4000, () => console.log('server running'))
Three cases where you need explicit type annotation
As a rule you should rely on type inference. In 3 specific cases you need to use explicit type annotation.
How to annotate a function
How to annotate an object
How to annotate a destructured value
How to annotate arrays
How to annotate tuples
How to create types in Typescript
How to create interfaces in Typescript
How to add method and property accessibility specifiers
How to use generics with constraints
How to use decorators
How to use React prop types with Typescript
How to use useRef instead of useState to represent a form
The usual approach for a form is to use state and set up change handlers on each input field. However this results on change handlers being fired on every keypress, potentially a large number of events. Since we are only interested in the contents of the input fields on a form submission, we can use refs to the input fields and only grab their values once when there is a form submission.
Last updated