¿Alguien puede explicar por qué el primer ejemplo funciona en TypeScript, pero el segundo ejemplo arroja un error? Aquí hay un ejemplo en vivo en un patio de juegos de Typescript.
const {type, payload} = action; switch(type) { ... //*** EXAMPLE 1 ***// case "CREATE_VARIABLE": if (payload == null || typeof payload === "string" || !("color" in payload)) return state; return createVariable(state, payload); ... //*** EXAMPLE 2 (Throws error in Typescript) ***// case "CREATE_VARIABLE": const isNotAVariable = (payload == null || typeof payload === "string" || !("color" in payload)); if(isNotAVariable) return state; return createVariable(state, payload); ... }El segundo ejemplo arroja el siguiente error de mecanografiado:
Argument of type 'string | Formula | Variable | Constant | undefined' is not assignable to parameter of type 'Variable | undefined'. Type 'string' is not assignable to type 'Variable | undefined'