All quizzesHard
Type-Level Programming — Series 3
Preview — 3 of 10 questions
javascript
type UserId = string & { readonly __brand: "UserId" };
type OrderId = string & { readonly __brand: "OrderId" };
declare const uid: UserId;
const a: string = uid;
const b: OrderId = uid;ABoth compile — the brands are erased, so all three types are structurally identical
BBoth fail — a branded type may not be assigned anywhere without a cast
Ca fails and b compiles — the brand blocks the widening but not the sibling assignment
Da compiles (UserId has every string member); b fails — the two brands are different literal types
javascript
const routes = {
home: "/",
user: "/users/:id",
} as const satisfies Record<string, `/${string}`>;
type UserRoute = typeof routes.user;AUserRoute is string
BUserRoute is "/users/:id" — as const keeps the literal, satisfies only validates it
CUserRoute is /${string} — satisfies replaces the inferred type with the target
DCompile-time error — as const and satisfies cannot be applied to the same expression
javascript
// strictFunctionTypes: true
interface WithMethod { handle(x: string): void }
interface WithProp { handle: (x: string) => void }
declare const m: WithMethod;
declare const p: WithProp;
const a: { handle(x: string | number): void } = m;
const b: { handle: (x: string | number) => void } = p;Aa compiles; b is a compile-time error — strictFunctionTypes checks property-syntax functions contravariantly, but leaves method shorthand bivariant
BBoth compile — strictFunctionTypes only affects standalone function types, never members
CBoth fail — a narrower parameter is never assignable to a wider one
Db compiles; a fails — method shorthand is the stricter of the two forms
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.