-
What is TypeScript?
- TypeScript is a strongly typed programming language that builds on JavaScript by adding static types. It compiles down to plain JavaScript and provides tools for catching errors early in development.
-
How does TypeScript differ from JavaScript?
- TypeScript adds static typing, interfaces, and additional features not present in JavaScript. JavaScript is dynamically typed, while TypeScript allows developers to specify types for variables, functions, and objects to catch errors before runtime.
-
What are the advantages of using TypeScript?
- The main advantages include early error detection through static typing, enhanced code readability, better tooling (like autocompletion and refactoring), and more maintainable code in large projects.
-
What is type inference in TypeScript?
- Type inference is when TypeScript automatically determines the type of a variable based on its initial value. For example, if you declare
let count = 5, TypeScript will infer thatcountis of typenumberwithout needing an explicit type annotation.
- Type inference is when TypeScript automatically determines the type of a variable based on its initial value. For example, if you declare
-
Explain the concept of "interfaces" in TypeScript.
- Interfaces define the shape or structure of an object. They specify what properties and methods an object should have, along with their types, but they do not implement any functionality. This helps ensure consistency across different objects.
-
How do you define a union type in TypeScript?
- A union type allows a variable to hold one of multiple types. You define it using the pipe (
|) symbol, likelet value: string | number. Here,valuecan be either a string or a number.
- A union type allows a variable to hold one of multiple types. You define it using the pipe (
-
What is an enum in TypeScript?
-
An
enumis a way to define a set of named constants, which can be either numeric or string values. It helps in making the code more readable and allows you to refer to values using descriptive names instead of numbers or strings.enum Color { Red, Green, Blue } let c: Color = Color.Green; // c is now 1
-
-
How do you define optional properties in TypeScript?
-
Optional properties are defined using a ? after the property name in an interface. This indicates that the property is not required, and the object can be created without it.
interface Person { name: string; age?: number; // optional }
-
-
What is the difference between interface and type in TypeScript?
- Both interface and type are used to define the shape of data. However, interface is used primarily for object structures, and it can be extended or implemented in classes. type can be used for creating more complex types like unions, intersections, or primitives, but it cannot be implemented by classes.
-
How does TypeScript handle
nullandundefined?
-
TypeScript treats
nullandundefinedas distinct types. When thestrictNullChecksoption is enabled,nullandundefinedmust be explicitly handled. This means they cannot be automatically assigned to other types likestringornumber. For example, a variable declared asstringcannot hold anullorundefinedvalue unless explicitly allowed.let name: string = "John"; name = null; // Error when `strictNullChecks` is enabled.
-
What is the difference between
interfaceandtypein TypeScript?- Both
interfaceandtypeare used to define the shape of data. However,interfaceis used primarily for object structures, and it can be extended or implemented in classes.typecan be used for creating more complex types like unions, intersections, or primitives, but it cannot be implemented by classes.
- Both
-
What are generics in TypeScript?
-
Generics allow you to create reusable components, functions, or classes that work with different data types while maintaining type safety. They act as placeholders for types, which are determined when the component is used.
function identity<T>(arg: T): T { return arg; }
-
-
How do you create a custom type in TypeScript?
- You use the
typekeyword to define custom types. This can be useful for creating type aliases for more complex types or unions.
type Point = { x: number, y: number };
- You use the
-
Explain the
anytype in TypeScript.- The
anytype can hold any kind of value, effectively opting out of type checking. While it provides flexibility, usinganyremoves the benefits of static typing and should be avoided in favor of more specific types whenever possible.
- The
-
What is the
nevertype in TypeScript?- The
nevertype represents values that never occur, such as functions that never return (like those that throw errors or run indefinitely). It indicates unreachable code paths.
function error(message: string): never { throw new Error(message); }
- The
-
How can you extend interfaces in TypeScript?
- You can use the
extendskeyword to create a new interface that builds on an existing one. This allows you to add or modify properties without repeating the entire structure.
interface Animal { name: string; } interface Dog extends Animal { breed: string; }
- You can use the
-
What is
readonlyin TypeScript?- The
readonlymodifier makes a property immutable, meaning it can be assigned only once (at the time of initialization) and cannot be changed afterward. It's useful for defining constants within objects.
interface Car { readonly make: string; }
- The
-
How do you declare a tuple in TypeScript?
- A tuple is an array with a fixed number of elements, where each element can have a different type. You declare it by specifying the types of each element in the array.
let tuple: [string, number]; tuple = ["hello", 10];
-
What are decorators in TypeScript?
- Decorators are special functions that can be attached to classes, methods, properties, or parameters to modify their behavior. They are typically used in frameworks like Angular for dependency injection and metadata annotations.
-
How do you define default parameters in TypeScript?
- Default parameters in functions are defined by providing a default value in the function signature. If no argument is passed for that parameter, the default value will be used.
function greet(name: string = "Guest") { return `Hello, ${name}`; }
-
How does TypeScript support modules and namespaces?
- TypeScript supports ES6-style modules using
importandexportto organize code into reusable pieces. Additionally, namespaces are used to group related code together, which is helpful for avoiding name collisions.
// file.ts export const pi = 3.14; // main.ts import { pi } from './file';
- TypeScript supports ES6-style modules using