cf.completefrontend

Write the codeMedium

Write isShape(value): a type guard for a discriminated union that arrives as unknown.

A drawing app loads shapes from JSON, so each one arrives as unknown. Write isShape so it returns true only for a circle with a number radius or a rect with a number width and height, and so TypeScript narrows the value to Shape when it does. Any other kind is rejected. Don't use as or any.

Write this
type Shape =  | { kind: "circle"; radius: number }  | { kind: "rect"; width: number; height: number }; function isShape(value: unknown) {  // Return true only for a valid circle or rect.  return false;} // const data: unknown = JSON.parse('{"kind": "circle", "radius": 2}');// if (isShape(data) && data.kind === "circle") console.log(data.radius);

Solve it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…