advanced

Conditional и template literal types

Вычисляемые types, infer, distributive conditionals и template literal types для string protocols.

Conditional types выбирают один type или другой по assignability и могут `infer` части type. Template literal types строят или разбирают string-shaped types. Вместе они моделируют event names, route params, CSS tokens и API naming conventions.

Conditional types distribute over naked type parameters в unions. Сложные recursive types замедляют compiler и путают команду — балансируйте precision и readability.

					type ApiRoute = `/api/${string}`;
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
				

На senior интервью: distributive behavior и когда type programming оправдан maintenance cost.

Типовые ошибки: путать похожие API и забывать edge cases в live coding.

Компромисс часто между ясностью и безопасностью или expressiveness и maintainability. Чеклист:

  • `extends` в conditional types.
  • `infer` для extracted parts.
  • Distribution over unions.