TypeScript Assignment- 5
Basic Questions
- Write a TypeScript example that uses as for type assertion to convert a variable of type unknown into string.
- Write a function combineToString that accepts number | boolean and uses type assertion to safely return the value as a string.
- Define an object with an index signature where keys are strings and values are numbers.
- Write a function that accepts an object with unknown keys (string keys, boolean values).
- Use mapped types to make all properties of an interface Person optional.
- Create a type ReadonlyCar from Car interface using mapped types.
- Use keyof to extract all keys of an interface Book.
- Define a type PropertyType<T, K extends keyof T> that extracts the type of a property.
- Write a conditional type that checks if a type is string → returns “Text”, else “Other”.
- Use infer in a conditional type to extract the return type of a function.
- Show an example of declaration merging with two interfaces of the same name User.
- Write a code snippet where a namespace groups utility functions.
- Import a function from another file using ES6 import syntax.
- Export a class using default export.
- Create a function logProperties<T>(obj: T) that logs all keys of the object using keyofand generics.
- Define a type alias using indexed access type T[K].
- Create an example of using union type with index signatures.
- Write a function that demonstrates a namespace merged with an interface.
- Define a namespace MathHelpers with a function square, and in the same file create a normal class Calculator that also uses square.
- Create a type that makes all properties of an interface Employee nullable using mapped types.
Intermediate Questions
- Create a function toNumber that accepts a value of type string | number and uses type assertion to convert it into number.
- Define an object with index signature where keys are numbers and values are strings.
- Create a generic function that uses keyof to access dynamic object properties safely.
- Write a mapped type Mutable<T> that removes readonly from all properties of a type.
- Define a mapped type Nullable<T> that makes all properties nullable.
- Create a conditional type IsArray<T> that checks if a type is an array.
- Use infer to extract the parameter types of a function.
- Write a type ReturnType<T> without using the built-in ReturnType.
- Demonstrate declaration merging with multiple interfaces Animal having different properties.
- Create an interface Shape with method area(). Then define a class Circle and Square implementing Shape. Demonstrate polymorphism by storing both in an array and calling area().
- Use modules to separate a MathUtils file (exported functions) and main.ts file (import).
- Show how namespace can be used to wrap a set of constants and enums.
- Create a module resolution scenario in tsconfig.json with “baseUrl” and “paths”.
- Write an example where namespace merges with a class.
- Use a conditional type to extract the element type from an array using infer.
- Create a type-safe function getProperty<T, K extends keyof T>(obj: T, key: K): T[K].
- Write a mapped type DeepReadonly<T> that makes all properties deeply readonly.
- Show a code snippet where a type uses both keyof and mapped types.
- Create an example where conditional type decides between two different interfaces.
- Use namespaces and modules together in a single project example.
Advanced Questions
- Write a type Flatten<T> using conditional types and infer that flattens nested arrays.
- Implement a generic utility type DeepPartial<T> that recursively makes all properties optional.
- Write a mapped type PickByValue<T, V> that picks only the properties of type V from T.
- Implement a conditional type IsNever<T> that checks if a type is never.
- Create a utility type PromiseType<T> that extracts the resolved type of a promise using infer.
- Write a generic classRepository<T> with methods add(item: T), getAll(), and find(predicate: (item: T) => boolean): T[].
- Build a type-safe EventEmitter interface using index signatures and mapped types.
- Write a namespace Database that contains interfaces, classes, and constants together.
- Implement a generic utility type DeepMutable<T> that recursively removes readonly from all properties, and test it with a nested object type.
- Implement a generic class ApiClient<T> that uses conditional types and mapped types to infer request/response shapes.