Illarion Koperski
Web developer, eCommerce specialist, Shopify expert
Illarion Koperski
2025-03-12
// Before
const a = JSON.parse('1') as string
// true at compile,
// but false at runtime
typeof a === 'string'// After
const _b = JSON.parse('1')
const b: number | null =
typeof _b === 'number' ? _b : null
// Before
declare global {
interface Window {
myGlobalStuff?: {
id: string
}
}
}
const id = window.myGlobalStuff?.id
// After
type CustomWindow = {
myGlobalStuff?: {
id: string
}
} & Window
declare let window: CustomWindow
const id = window.myGlobalStuff?.id
export const infer = (input: 'a' | 'b' | 'c') => {
switch (input) {
case 'a':
return 'x'
case 'b':
return 'x'
default:
// @ts-expect-error We forgot to handle 'c'
input satisfies never
return 'z'
}
}
https://www.totaltypescript.com/ts-reset
interface MyThing {
id?: string
}
interface MyThing {
name?: string
}
const a: MyThing = { id: '1' }
const b: MyThing = { name: 'Larry' }
const c: MyThing = { id: '1', name: 'Larry' }
// LOOK MA, NO ERRORS!
github.com/illarionvk/warsawjs-2025-02-typescript
Can we adopt Typescript gradually?
Can I build a chess game with this thing?
What about branded types?
By Illarion Koperski