All quizzesHard
Conditional Types & Infer — Series 3
Preview — 3 of 10 questions
javascript
type IsStr<T> = T extends string ? "y" : "n";
type A = IsStr<unknown>;
type B = IsStr<any>;
type C = IsStr<never>;AA is "y", B is "y", C is "n"
BA is "n", B is "y" | "n", C is never
CAll three are "n"
DA is "n", B is "y", C is "n"
javascript
function label<T extends string | number>(value: T): T extends string ? "s" : "n" {
if (typeof value === "string") {
return "s";
}
return "n";
}ACompiles fine — narrowing value resolves the conditional in each branch
BCompiles fine — a conditional return type is checked only at call sites
CCompile-time error — a function's return type may not be a conditional type
DCompile-time error on both return statements — while T is unresolved the conditional stays deferred, and no concrete type is assignable to it
javascript
type SplitFirst<S extends string> = S extends `${infer A}-${infer B}` ? [A, B] : never;
type SplitChar<S extends string> = S extends `${infer A}${infer B}` ? [A, B] : never;
type X = SplitFirst<"a-b-c">;
type Y = SplitChar<"abc">;AX is ["a", "b-c"], Y is ["a", "bc"]
BX is ["a-b", "c"], Y is ["ab", "c"]
CX is ["a", "b"], Y is ["a", "b"] — the trailing text is discarded
DBoth are never — a template literal pattern must match the whole string exactly with no ambiguity
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.