“sindicatos mecanografiados” Código de respuesta

sindicatos mecanografiados

type MyBool = true | false;
Puzzled Puffin

sindicatos de mecanografiado discriminados

// Object unions with variants of a similiar property, but different
// key names. Need to add a common property to connect them.
interface Circle {
  kind: "circle";
  radius: number;
}
 
interface Square {
  kind: "square";
  sideLength: number;
}
 
type Shape = Circle | Square;

// TypeScript can now narrow out the members
function getArea (shape: Shape) {
  if (shape.kind === "circle") {
    return Math.PI * shape.radius ** 2 // safely accessing shape.radius
  }
}

// Alternatively use a switch statement
switch(shape.kind) {
  case 'circle':
    return Math.PI * shape.radius ** 2
  case 'square':
    return shape.sideLength ** 2
Stupid Scarab

Respuestas similares a “sindicatos mecanografiados”

Preguntas similares a “sindicatos mecanografiados”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código