Core TypeScript syntax including types, interfaces, and generics for developers coming from JavaScript.
35 cards · basic cards · AI-written, checked twice. Edit anything.
- What is TypeScript?
- A typed superset of JavaScript that compiles to plain JavaScript, adding static type checking at development time.
- What does the colon (:) syntax do in TypeScript?
- It declares a type annotation for a variable, parameter, or return value.
- How do you annotate a variable as a string in TypeScript?
- Use const name: string = 'value';
- What is the number type in TypeScript?
- The type for all numeric values, including integers, floats, Infinity, and NaN.
- What is the boolean type in TypeScript?
- The type for true and false values.
- What does the 'any' type mean in TypeScript?
- A type that allows any value and disables type checking for that variable, defeating TypeScript's type safety.
- What is the 'unknown' type in TypeScript?
- A safe counterpart to 'any': a variable of unknown type cannot be used without first narrowing its type.
- What is the 'never' type in TypeScript?
- The type that represents a value that will never occur, used for functions that never return or throw errors.
- What is the difference between null and undefined in TypeScript?
- null is explicitly set to mean no value, undefined means a variable has been declared but not assigned.
- What is a union type in TypeScript?
- A type that can be one of several types, written as Type1 | Type2, representing multiple possible types.
- What is an intersection type in TypeScript?
- A type that combines multiple types using &, requiring a value to satisfy all types at once.
- What is a type alias in TypeScript?
- A way to define a new name for a type using the 'type' keyword, e.g., type ID = string | number;
- How do you define an interface in TypeScript?
- Use the 'interface' keyword followed by the name and property definitions inside braces.
- What is the main difference between an interface and a type alias?
- Interfaces can be merged and extended, while type aliases cannot; types support unions and intersections, interfaces do not.
- How do you make a property optional in an interface?
- Add a question mark (?) after the property name, e.g., name?: string;