-
What is a generic function in TS
- way to write code that is Type flexible without using “any”
function genericFunction<T>(arg: T) {
return "string" as T
}
-
what is “as const” in TS and diff bw this and normal “const”
- allows us to write objects that are sealed in a way that you cannot modify any part of the object, ex. DB config
- TS is a developer tool → everything applies during build time
- JS Object.freeze applies at runtime and only at first level → does not apply to nested data
const config = {
host: "123",
port: 45
}
// can be modified
config.host = "456"
const config = {
host: "123",
port: 45,
auth: {
client: "abc"
}
} as const; // nested fields and objects are immutable now
-
what does the private access modifier do when added to a class
- private → cannot be accessed outside of class → interface segregation
-
TS decorator, what is it and what is the use case
- way to reuse code following decorator pattern
- class inheritance → violates liskov substitution → creates messy chain
// apply code to multiple classes
function Injectable(){}
@Injectable
class Service {...}
-
difference bw type vs. interface
- at build time interfaces are merged
- types have to be unique → duplicate types will give type error
- request type - interface - extend - props/auxiliary
- type - define domain entities
-
type guard
- constraints added to filter types
-
structural vs nominal typing
- TS → structural typing - if 2 objects have the same properties then they are the same - interchangeable - gives flexibility to combine and interchange objects if they satisfy the same properties
- Java/C# → nominal typing - objects must match same signature to be the same, have to be instances of the same class