Keys
User is an ordinary object type with three keys: id, name and admin. Each key has its own type: number, string and boolean. Line 1 writes it in code. Everything that follows builds a new type from this one.
keyof User is a type made of User's key names: "id" | "name" | "admin". The bar | means "one of these", so this is a union of three string literal types. Nothing runs: tsc reads the keys straight off the type.
The mapped type
Line 4 is a mapped type. [K in keyof T] works like a for loop over the keys: for each key K it adds one property to a new object type. So the new type has the same three keys; their types come next.
The conditional type
First K is "id". T[K] is an indexed access type: it looks up the type of key K inside T, the way obj[key] looks up a value. T is User, so T["id"] is number. That type goes to the test.
T[K] extends number ? string : T[K] is a conditional type, an if for types. extends asks: is the left type assignable to number? For id, number is, so the first branch wins and id becomes string.
Next "name": string is not a number, so the test says no and the second branch keeps T[K], which is string. "admin" goes the same way: boolean stays boolean. Each key is tested on its own.
Using the new type
Line 6 applies AsText to User. Form is { id: string; name: string; admin: boolean }. Nobody wrote that by hand: tsc computed it. If User gains a key later, Form gains it too.
Line 7 makes an object of type Form, but writes id as the number 42. Form says id is a string, so tsc reports error TS2322: Type 'number' is not assignable to type 'string'.
Write id as the text "42" and the object matches Form, so tsc reports no errors. Like every type, Form is erased when tsc writes JavaScript: at run time f is a plain object.
What happens
;; step 1/9 · source
> 1 type User = { id: number; name: string; admin: boolean };
key id type number
key name type string
key admin type boolean
The built-in utility type Partial<T> is a mapped type too: TypeScript defines it as type Partial<T> = { [P in keyof T]?: T[P] }; The ? after the key makes every property optional; readonly in front of [P in keyof T] makes every property read-only. When the type being tested is a bare type parameter, such as T in T extends string ? ..., a conditional type is applied to each member of a union separately ("distributive"). Here the tested type is T[K], not a bare T, so boolean is tested as a whole and stays boolean.