Video guide
13:46 · 25 April 2024
TypeScript Interview Questions & Answers
Chapters 7 sections
Interview questions in this video:
- 01
1. What is a generic function in TypeScript?
A function that takes type arguments alongside value arguments — type-flexible code without falling back to any.
Declare function myFn<T>(param: T) and the caller fixes T at use time: myFn<number>(...) makes anything but a number a compile error. Use generics when the type is only known by the caller.
GenericsType arguments - 02
2. What does 'as const' do, and how is it different from Object.freeze?
as const makes an object literal deeply read-only at the type level — every nested property becomes immutable to the compiler. Perfect for config objects nobody should mutate.
Object.freeze is the runtime cousin, but it's shallow (first level only) and does nothing at build time. as const is compile-time only — it won't stop code that's already running.
as constImmutabilityBuild time vs runtime - 03
3. What does the private access modifier do?
It makes a class member accessible only inside the class — instances can't reach it from outside, and TypeScript errors if they try.
It's interface segregation in practice: expose exactly what consumers of the class need, nothing more.
Access modifiersEncapsulationSOLID - 04
4. What is a decorator, and when would you use one?
A function that wraps a class or method to add shared behavior — @Injectable() in NestJS or Angular is the classic case.
It's the decorator pattern: composition over inheritance. Reusing behavior through long inheritance chains violates Liskov substitution sooner or later; decorating avoids the chain entirely.
DecoratorsNestJSComposition over inheritance - 05
5. type vs interface — what's the difference, and when do you use each?
Mostly interchangeable, with one real difference: interfaces merge. Declare the same interface twice and TypeScript combines them; types must be unique.
That makes interfaces right for anything consumers might extend — like Express's Request. A practical split: types for your domain entities, interfaces for auxiliary shapes like component props.
Declaration mergingDomain modeling - 06
6. What is a type guard?
A function that narrows a union type: given type Fruit = Pineapple | Apple | Strawberry, an isPineapple() guard tells the compiler which member you're holding.
Use one whenever you need to do something specific with one member of a union.
Type narrowingUnion types - 07
7. Structural vs nominal typing — which does TypeScript use?
Structural. If two objects have the same shape, TypeScript treats them as the same type.
Java and C# are nominal: same properties mean nothing unless the objects share the declared class. Structural typing gives TypeScript its flexibility — objects are interchangeable when they satisfy the same shape.
Structural typingType compatibility
Transcript
Hi folks, we are back with Bogdan today. And today we're going to go through some senior TypeScript interview questions. Let's go with the first question. What is a generic function in TypeScript and can you write one? So I'm going to share my screen. I have a code sandbox over here. Perfect. So generic generic functions. Basically it's a way to write code that it's type flexible without using any. So what do I mean by that?
You can basically the generic function provide not only normal arguments but also type arguments. So we have a function. Let's call this my new function and it can actually accept here a bunch of types. It can be many of them. Let's say T UK. That we can use inside the function. So for example, we could actually return um something like like let's return a string as the type T for example. So of course this is not a very creative example but this is a type argument and it means that this function can actually work with different arguments.
Um to make this more interesting you could actually say we have a parameter here and it's of type T and so I could actually create, you know, like a result. Let's say my first result would be calling that new function with a number type as type argument and then whatever I provide has to be number. If I put here a string it won't work. Because I used this type as something in my uh yeah, in my code. So basically they allow you to whenever you need a function to support or to work with different types or you don't know the type already. You only will know the type um of arguments or whatever the
return is when you use this function. It allows whoever is using the function to provide that type as a type argument. Awesome, yeah. Well, this is why they are called generic types, right? Generic functions. Um awesome second question. Let's move on. Wait, the second question which is what is a as const in types and what would the difference be with a normal const when used? Okay, so as const, I think that's a relatively new feature.
So basically it will allow us to it is basically allowing us to write objects that are sealed in a way that you cannot modify any part of the object. Because traditionally in JavaScript whenever we have let's say a config object, that's a very good example, and I have something here like a let's say a host. Let's say imagine this will be a database config, blah blah blah. And then I'm going to add a port.
Um even if this is a constant, like we added here constant, it doesn't really matter. Someone could go in and modify a property on like it they could modify the host. I could actually say config .host it's equal to whatever. Something else. And and and you assign it, right? Exactly. So this is a problem, especially in the back end when you have a config object you don't want people to modify it. And back in JavaScript you could freeze the object, but it would only work in the first level. So I think right now with as const all of the sudden every nested property will also be immutable. So you see I get
now this type error that says you cannot modify that, and it works a couple of levels deep. So imagine here we would have some auth service configuration, and here we have a client. And nested objects, right? It works for nested objects as well. Yes, so if I do this, auth.client, I'll get an error. It will propagate. Yeah. Um now you mentioned something. You said you could actually in JavaScript you could for the first level of the object, right? You could actually use uh you could actually freeze the object. Can you be more Mhm.
Can you you could detail that? How would you do that? Yeah, so before TypeScript it could use object.freeze to freeze this object and no one could actually modify it. But the the difference is this it's only at build time. So whenever we compile the code this will be evaluated. Whereas object.freeze it's really evaluated at run time. So this is JavaScript code and so it will actually work also when you actually run the code in production.
Again, TypeScript is only a developer tool. So this it's just a way it's going to break the build. It won't compile. Uh but if you were to execute the code it will still execute. The other thing is object.freeze only works at the first level. So it doesn't work nestedly. Awesome bug. Then we're going to move on with question number three. What does the private access modifier do when added to a class variable or a method?
Okay, so the private access modifier. I'm going to share my screen and try to code this out because it's easier to think about it like that. Um okay, so let me just drive write down a quick class. So imagine we have a service and it has So we mentioned private, right? Private. What can be private? Well, imagine we have some sort of configuration and it's of type string. And so let me instantiate this.
Perfect. And it just gets um test whatever. Um perfect. So this is our class and basically because because the configuration it's private, you won't be able to access it from the outside. That means if someone will um create an instance of this class by using the constructor signature and instantiate the service, they cannot really do new service.config. That won't work. And TypeScript actually tells us, "Hey, this is private. It's only accessible within that class." And it's a very good way to apply the solid principle of interface segregation and really just exposing from a class the things that
whoever is using that class needs and nothing more. TypeScript is looking more and more like Java, right? Um cool. Nice, nice. Um great answer. I mean, yeah, you know, we don't have native class cons so native classes in JavaScript, so TypeScript is kind of trying to to add all that. Um Awesome. We can move on now with question number five, yeah? And we will talk about decorators. What is a decorator in TypeScript and, you know, what would be the use case? When would you use a decorator? Um and if you can provide any examples, just feel free.
Awesome. So, uh decorators are nothing new. You have decorators in most object-oriented languages, and it's just a way to basically reuse reuse code um following the decorator pattern, which basically means we just wrap a certain function or class with some code that will add some share um functionality. We do that a lot in NestJS, and uh well, all of Angular and all of those frameworks. Basically, a decorator is a function that can actually decorate like this, let's say, this class we want to make it a control a service or injectable. This is a very use case, uh common use case in frameworks like
NestJS. Um and basically, this at injectable it's actually some sort of function that takes, you know, takes whatever this class before it was instantiated, and it can modify the constructor, apply some changes, and we can then reuse this and apply it to as many classes as we want. So, it's basically a way to reuse code, and you can decorate classes, and you can also decorate functions. Yeah. If we think about the front end, it it reminds me of recompose when people were using something that looks really like a decorator to to add extra functionality, right? A higher-order function, right?
To add extra functionality to to another function. Um yes, the cool thing here is that you don't have class inheritance. So because normally you could add functionality by using class inheritance, but that will end up via violating the Liskov principle, which means you'll end up with if you add if you only use logic by having classes inheriting from each other, you end up with a very long prototypical chain and you end up violating the Liskov substitution principle. So it's a bit more of going to direction of composition over inheritance. Cool, I like that you you dive deep into the solid principles to to
answer this one. Question number six, what is the difference between type and interface in TypeScript and when would you use one or the other? Very common senior back-end interview question for TypeScript engineers. Sure. I think this is a one of the oldest oldest questions. So you could interchange both for the most part. The only difference is at build time interfaces are merged, which means you can have the same interface declared in many parts of the code and it will get kind of merged at build time. Whereas types have to be unique.
One type it's it's singular and if you have another type with the same name, you'll get TypeScript error. What does that mean for developer? It means if you're building a library where you might want people to extend certain interfaces. So for example, imagine I'm building Express JS and someone needs to extend the request object, it's much better the request type is much better if I provide it as an interface because then they can actually extend that with whatever fields they want to add to the request object and then TypeScript will figure it out. It will take my interface, their interface and merge
them together. Whereas with types, that's not the case. So to answer your question, when would I use one or the other? I would use types to define your domain entities. Domain entities as in if you have a an e-commerce website for example, the products, the prices and so on. And then I would use interfaces for anything else that just They are just auxiliary shapes that define, for example, the props of a React component or the input of a certain function. You don't really need a type for that. You can just use interfaces. Okay, now we're going to move on with the next question. What is a type guard in TypeScript? And, you
know, when would you use a type guard? No need to code for this one. Okay, um so a type guard is basically a way to write a function that will be able to tell you whenever you have a union type, which is a type that it's made up of different types. So, something can be Well, imagine you have a type fruit, and it can be either a uh pineapple or an apple or a strawberry. Uh and you get that type fruit, but you need to really know if it's a pineapple. Then you could code a is pineapple type guard that will actually tell you whenever you get a fruit, is this a pineapple?
Cool. Um so, it's basically a a function to guard certain types. Like, if you you just want to filter out other types. Um cool. Uh when when would you use one if you if you have any any examples in mind, any quick examples? So, as I mentioned, whenever you get a list of a type that's a union type, but you want to do something specific with that object that you can only do with one type, one sub type of that union, uh and you need to figure out again, you know, is this a pineapple rather than is it a fruit, you want to narrow the type of that object, then you'd use one of those.
Awesome. And finally, next question. Uh how is structural typing different than nominal typing, and which one does TypeScript really implements? Uh very very good question. Um So, TypeScript it's basically implementing structural typing, which means if two objects have the same property, then they are the same thing from for TypeScript. Um but if you go to languages like Java or C#, you'll see they implement mostly nominal typing.
That means really for something to be of a certain type, it has to really match the same signature. So, if I have a class, imagine we have like the class before that I used service, if you really compare a service to another, they really have to be instances of the same class. It doesn't matter they have the same properties, if they were not instantiated by a class with the same name, it won't work. So, the cool thing in TypeScript is that it gives you certain flexibility because you can always combine objects or interchange them if they satisfy the same properties.
Yeah, as always with JavaScript, we have more freedom than with strictly typed languages like the ones you mentioned. Um amazing Bogdan, thank you so much. This is it for today. TypeScript senior TypeScript interview questions. Now, uh for the people watching us, for you watching us developers, number one, if you have any TypeScript interview question or any kind of back front and full stack interview questions you want Bogdan and I to address in one of those videos, just, you know, drop it in the comments and we will pick it up and add it to one of our next videos.
Uh secondly, if you are looking to join a ambitious community of software engineers that are working to get to the next level with quality materials, resources, feedback, monthly calls with Bogdan and I, then click the free community that we put together in school. You can apply to join us and if we see your application makes sense, then we're going to get you in. Now, I ask you to be patient. We have a lot of people applying lately.
And finally, if you want Bogdan and I to personally mentor you towards the senior level and beyond, finding out your technical gaps, you know, putting in place a step-by-step strategy to fill those gaps and build you into a confident, competent full stack front end senior engineer, right? Getting, you know, your the responsibility you can handle, the salary, everything, getting your um your developer career to a whole new level, then just apply on the link in comments. You're going to book a call with me, short call. We're just going to get to know each other, see where I got where you're at, see if we can help you,
and then we can decide uh to move forward or not. With that being said, uh we will see you all in the next one. Take care. Thank you, Bogdan. And yeah, stay cool.