You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Item 3: Understand That Code Generation Is Independent of Types
Things to Remember
Code generation is independent of the type system. This means that TypeScript types cannot affect the runtime behavior of your code.
It is possible for a program with type errors to produce code ("compile").
TypeScript types are not available at runtime. To query a type at runtime, you need some way to reconstruct it. Tagged unions and property checking are common ways to do this.
Some constructs, such as class, introduce both a TypeScript type and a value that is available at runtime.
Because they are erased as part of compilation, TypeScript types cannot affect the runtime performance of your code.
Code Samples
interfaceSquare{width: number;}interfaceRectangleextendsSquare{height: number;}typeShape=Square|Rectangle;functioncalculateArea(shape: Shape){if(shapeinstanceofRectangle){// ~~~~~~~~~ 'Rectangle' only refers to a type,// but is being used as a value herereturnshape.height*shape.width;// ~~~~~~ Property 'height' does not exist on type 'Shape'}else{returnshape.width*shape.width;}}
functionsetLightSwitch(value: boolean){switch(value){casetrue:
turnLightOn();break;casefalse:
turnLightOff();break;default:
console.log(`I'm afraid I can't do that.`);}}