Workshop 4SH
À quoi ressemblerait une application fullstack "monoglote" ?
🤮Webpack, les ORMs
😍JOOQ, Mapstruct
function handleLang(lang) {
// later...
const country = lang
// ⬆️ Seems legit with "fr" doesn't it ? 😱
}
handleLang("fr")
// Better...
type LanguageCode = "fr"|"en"|"sp"
type CountryCode = "fr"|"uk"|"sp"
function handleLang(lang: LanguageCode) {
// later...
const country: CountryCode = lang
// ⬆️ Doesn't compile
// Type 'LanguageCode' is not assignable to type 'CountryCode'.
// Type '"en"' is not assignable to type 'CountryCode'.
}
handleLang("fr")
@Controller("checkins")
export class CheckinsController {
constructor(private readonly appService: AppService) {}
@Post("/:year")
async createThenGetAllCheckins(
@Param("year", ParseIntPipe) year: number,
@Body() checkins: Checkin[]
): Promise<Checkin[]> {
const persistedCheckins = await this.appService.createThenGetAllCheckins(year, checkins);
return persistedCheckins;
}
}
Quick setup for Frontend+Backend
// Exporting
export const varName = "Hello world"
// Consuming
import {varName} from "module-name"
// Exporting
module.exports = {
varName: "Hello world"
}
// Consuming
const moduleName_1 = require("module-name")
const varName = moduleName_1.varName
// tsconfig.json
{
...
"compilerOptions": {
"module": "ES2015",
"outdir": "dist/mjs"
...
}
}
// package.json du module mjs produit
{
"type": "module"
...
}
// package.json du module cjs produit
{
"type": "commonjs"
...
}
// package.json
{
"name": "my-shared-module",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"types": "dist/mjs/index.d.ts",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
"clean": "rm -fr dist/* && mkdir -p dist/cjs dist/mjs",
"build": "npm run clean && concurrently \"npm run build:mjs\" \"npm run build:cjs\"",
"build:mjs": "tsc -p src/ && ./scripts/fixup-dist-mjs-module",
"build:cjs": "tsc -p src/ --module commonjs --outdir dist/cjs && ./scripts/fixup-dist-cjs-module"
},
...
}
// tsconfig.json from shared module
{
"compilerOptions": {
...
"composite": true
...
}
}
// tsconfig.json from UI|SRV modules
{
"compilerOptions": {
...
"moduleResolution": "node",
"paths": {
"@shared/*": ["../shared/dist/mjs/*"]
}
...
},
"references": [
{ "path": "../shared/src/tsconfig.json" }
]
}
import {Hello} from '@shared/domains'
const hello: Hello = new Hello();
Setup d'un shared module
En parcourant le code de kermesse-tracker...
Le source des live-coding :
https://github.com/fcamblor/kermesse-tracker
https://github.com/fcamblor/fullstack-ts-demo
Fullstack Typescript - Workshop 4SH