Primitive annotations are usually inferred from the assignment
let name: string = 'web'
let port: number = 8080
let ok: boolean = trueTypeScript syntax reference: primitives and literal types, interface vs type, generics and constraints, conditional types with infer, built-in utility types, plus strict tsconfig and tsc/tsx CLI usage.
Primitive annotations are usually inferred from the assignment
let name: string = 'web'
let port: number = 8080
let ok: boolean = trueunknown must be narrowed before use and is safer than any
let input: unknown = JSON.parse(raw)
if (typeof input === 'string') input.trim()never means no normal return; useful for throwing helpers and exhaustiveness checks
function fail(msg: string): never {
throw new Error(msg)
}Literal unions constrain values; intersections emulate branded types
type Env = 'dev' | 'prod'
type Id = string & { readonly brand: unique symbol }as asserts without runtime effect; satisfies checks while keeping precise inference
const value = config as AppConfig
const safe = obj satisfies AppConfigInterfaces suit object shapes and can merge; ? makes optional, readonly locks fields
interface User {
id: string
name?: string
readonly email: string
}type fits unions, intersections and function aliases but cannot be merged
type Point = { x: number; y: number }
type Handler = (event: Event) => voidIndex signatures describe dynamic keys; Record is clearer for known shapes
interface Dict {
[key: string]: number
}Declare overload signatures first, then a single implementation
function parse(input: string): string
function parse(input: number): number
function parse(input: string | number) {
return typeof input === 'string' ? input.trim() : input
}Constraints require the needed fields while preserving the original type
function first<T extends { id: string }>(items: T[]): T | undefined {
return items[0]
}keyof yields the union of keys; typeof extracts a value type
type Keys = keyof User
type UserType = typeof defaultUserMapped types transform all properties; adjust with -? or +readonly
type Optional<T> = { [K in keyof T]?: T[K] }Conditional types with infer extract element types from arrays
type ElementType<T> = T extends (infer U)[] ? U : neverUtility types compose to describe partial updates and projections
type Patch = Partial<Pick<User, 'name' | 'email'>>
type Summary = Omit<User, 'email'>
type Map = Record<string, number>Extract return types and class instance types when wrapping libraries
type Return<T> = T extends (...args: never[]) => infer R ? R : never
class Store {}
type StoreInstance = InstanceType<typeof Store>strict enables all checks; noUncheckedIndexedAccess makes indexing return possible undefined
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true
}
}target controls downleveling while lib controls available built-in types
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"lib": ["ES2022", "DOM"]
}
}Path aliases affect type resolution only; mirror them in the bundler
{
"compilerOptions": {
"baseUrl": ".",
"paths": { "@/*": ["src/*"] }
}
}skipLibCheck skips dependency declarations to speed up type checking
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true
}
}Generate a tsconfig.json template and prune unneeded options
npx tsc --initType-check without emitting, ideal for CI and lint stages
npx tsc --noEmitRun TypeScript directly; tsx is faster than ts-node and ESM-friendly
npx tsx scripts/build.tsAdd declarations for untyped modules in a .d.ts file
declare module '*.vue' {
const component: DefineComponent<{}, {}, any>
export default component
}This website uses cookies to ensure you get the best experience on our website. Privacy Policy.