{"id":20875334,"url":"https://github.com/aershov24/typescript-interview-questions","last_synced_at":"2025-05-09T00:08:51.301Z","repository":{"id":44887048,"uuid":"235287067","full_name":"aershov24/typescript-interview-questions","owner":"aershov24","description":"🔴 TypeScript Interview Questions and Answered to prepare for your next Web/Angular developer interview","archived":false,"fork":false,"pushed_at":"2022-01-20T11:36:07.000Z","size":17,"stargazers_count":242,"open_issues_count":2,"forks_count":46,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-09T00:08:45.155Z","etag":null,"topics":["interview-practice","interview-preparation","interview-questions","interviewing","javascript","typescript"],"latest_commit_sha":null,"homepage":"https://www.fullstack.cafe/TypeScript","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aershov24.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-21T08:09:44.000Z","updated_at":"2025-05-08T13:54:52.000Z","dependencies_parsed_at":"2022-09-21T13:01:02.964Z","dependency_job_id":null,"html_url":"https://github.com/aershov24/typescript-interview-questions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aershov24%2Ftypescript-interview-questions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aershov24%2Ftypescript-interview-questions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aershov24%2Ftypescript-interview-questions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aershov24%2Ftypescript-interview-questions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aershov24","download_url":"https://codeload.github.com/aershov24/typescript-interview-questions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166521,"owners_count":21864482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interview-practice","interview-preparation","interview-questions","interviewing","javascript","typescript"],"created_at":"2024-11-18T06:44:06.834Z","updated_at":"2025-05-09T00:08:51.272Z","avatar_url":"https://github.com/aershov24.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# TypeScript Interview Questions and Answers\n\nTypeScript starts from the same syntax and semantics that millions of JavaScript developers know today. Don't miss that advanced list of TypeScript interview questions and answers and nail your next web developer tech interview in style.\n\n\u003e You could also find all the answers here 👉 https://www.fullstack.cafe/TypeScript.\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.fullstack.cafe\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/13550565/73042889-e7533900-3e9d-11ea-94f2-b4a9e87cc018.png\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n### Q1: List the built-in types in Typescript ⭐\n\n**Answer:**\n\nThese are also called the primitive types in TypeScript:\n* **Number** type: it is used to represent number type values and represents double precision floating point values.\n```js\nvar variable_name: number;\n```\n* **String** type: it represents a sequence of characters stored as Unicode UTF-16 code. It is the same as JavaScript primitive type.\n```js\nvar variable_name: string;\n```\n* **Boolean** type: in Typescript, it is used to represent a logical value. When we use the Boolean type, we get output only in true or false. It is also the same as JavaScript primitive type.\n```js\nvar variable_name: bool;\n```\n* **Null** type: it represents a null literal and it is not possible to directly reference the null type value itself.\n```js\nvar variable_name:number = null;\n```\n* **Undefined** type: it is the type of undefined literal. This type of built-in type is the sub-type of all the types.\n```js\nvar variable_name:number = undefined;\n```\n\n\n🔗 **Source:** [FullStack.Cafe](https://www.fullstack.cafe)\n\n\n### Q2: What are Modules in Typescript? ⭐\n\n**Answer:**\n\nModules in Typescript helps in organizing the code. There are 2 types of Modules — Internal and External\n\n* **Internal Modules** are now replaceable by using Typescript’s namespace.\n\n* **External Modules** used to specify and load dependencies between multiple external js files. If there is only one js file used, then external modules are not relevant.\n\n🔗 **Source:** [FullStack.Cafe](https://www.fullstack.cafe)\n\n\n### Q3:  What is Typescript and why one should use it? ⭐\n\n**Answer:**\n\nTypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language.\n\n🔗 **Source:** [FullStack.Cafe](https://www.fullstack.cafe)\n\n\n### Q4: Explain generics in TypeScript ⭐\n\n**Answer:**\n\nGenerics are able to create a component or function to work over a variety of types rather than a single one.\n\n```js\n/** A class definition with a generic parameter */\nclass Queue\u003cT\u003e {\n  private data = [];\n  push = (item: T) =\u003e this.data.push(item);\n  pop = (): T =\u003e this.data.shift();\n}\n\nconst queue = new Queue\u003cnumber\u003e();\nqueue.push(0);\nqueue.push(\"1\"); // ERROR : cannot push a string. Only numbers allowed\n\n```\n\n\n🔗 **Source:** [basarat.gitbooks.io](https://basarat.gitbooks.io/typescript/docs/types/generics.html)\n\n\n### Q5: What is TypeScript and why would I use it in place of JavaScript? ⭐\n\n**Questions Details:**\n\n\n\n\n\n**Answer:**\n\n**TypeScript** is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as *you type the code*. For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.\n\nIn details:\n* TypeScript supports new ECMAScript standards and compiles them to (older) ECMAScript targets of your choosing. This means that you can use features of ES2015 and beyond, like modules, lambda functions, classes, the spread operator, destructuring, today. \n* JavaScript code is valid TypeScript code; TypeScript is a superset of JavaScript. \n* TypeScript adds type support to JavaScript. The type system of TypeScript is relatively rich and includes: interfaces, enums, hybrid types, generics, union and intersection types, access modifiers and much more. TypeScript makes typing a bit easier and a lot less explicit by the usage of type inference.\n* The development experience with TypeScript is a great improvement over JavaScript. The IDE is informed in real-time by the TypeScript compiler on its rich type information. \n* With strict null checks enabled (`--strictNullChecks` compiler flag) the TypeScript compiler will not allow undefined to be assigned to a variable unless you explicitly declare it to be of nullable type. \n* To use TypeScript you need a build process to compile to JavaScript code. The TypeScript compiler can inline source map information in the generated .js files or create separate .map files. This makes it possible for you to set breakpoints and inspect variables during runtime directly on your TypeScript code. \n* TypeScript is open source (Apache 2 licensed, see github) and backed by Microsoft. *Anders Hejlsberg*, the lead architect of C# is spearheading the project.\n\n🔗 **Source:** [stackoverflow.com](https://stackoverflow.com/questions/12694530/what-is-typescript-and-why-would-i-use-it-in-place-of-javascript)\n\n\n### Q6: What is TypeScript and why do we need it? ⭐\n\n**Answer:**\n\nJavaScript is the only client side language universally supported by all browsers. But JavaScript is not the best designed language. It’s not a class-based object-oriented language, doesn’t support class based inheritance, unreliable dynamic typing and lacks in compile time error checking. And TypeScript addresses all these problems. In other words, TypeScript is an attempt to “fix” JavaScript problems.\n\nTypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds **optional static typing** and **class-based object-oriented programming** to the language. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages. At the end of day “TypeScript is a language that generates plain JavaScript files.”\n\nAs stated on [Typescript official website](http://www.typescriptlang.org/), “TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.” Where “**typed**” means that it considers the types of variables, parameters and functions.\n\n🔗 **Source:** [talkingdotnet.com](http://www.talkingdotnet.com/typescript-interview-questions/)\n\n\n### Q7: What are the benefits of TypeScript? ⭐\n\n**Answer:**\n\nTypeScript has following benefits.\n\n*   It helps in code structuring.\n*   Use class based object oriented programming.\n*   Impose coding guidelines.\n*   Offers type checking.\n*   Compile time error checking.\n*   Intellisense.\n\n🔗 **Source:** [talkingdotnet.com](http://www.talkingdotnet.com/typescript-interview-questions/)\n\n\n### Q8: Do we need to compile TypeScript files and why? ⭐\n\n**Answer:**\n\nYes we do. Typescript is just a language Extension browsers can't interpret it. Converting from TypeScript to JavaScript is called compiling. Compiling doesn't mean binary code is created in this case. For this kind of translation, also the term transpilation is used instead of compilation.\n\n🔗 **Source:** [stackoverflow.com](https://stackoverflow.com/questions/45125284/why-is-angular-compiled)\n\n\n### Q9: How to call base class constructor from child class in TypeScript? ⭐\n\n**Answer:**\n\nWe can call base class constructor using `super()`.\n\n🔗 **Source:** [http://www.talkingdotnet.com](http://www.talkingdotnet.com/typescript-interview-questions/)\n\n\n### Q10: What are the difference beetween Typescript and JavaScript? ⭐⭐\n\n**Answer:**\n\n* It is an object oriented programming language (not pure).\n* Here it is static typing (We can declare a variable in multiple ways). ex: var num : number.\n* It has interfaces.\n* It has optional parameter feature.\n* It has Rest Parameter feature.\n* Supports generics.\n* Supports Modules\n* Number, string etc. are the interfaces.\n\n\n🔗 **Source:** [FullStack.Cafe](https://www.fullstack.cafe)\n\n\n### Q11: What is Interface in TypeScript? ⭐⭐\n\n**Answer:**\n\nOne of TypeScript’s core principles is that type-checking focuses on the *shape* that values have.\n\nAn `interface` is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes.\n\nWhen you define your interface you’re saying that any object (not an instance of a class) given this contract must be an object containing interfaces properties.\n\n🔗 **Source:** [medium.com](https://medium.com/front-end-hacking/typescript-class-vs-interface-99c0ae1c2136)\n\n\n### Q12: When to use interfaces and when to use classes in TypeScript? ⭐⭐\n\n**Answer:**\n\nIf you need/wish to create an instance of perhaps a custom object, whilst getting the benefits of type-checking things such as arguments, return types or generics - a class makes sense. \n\nIf you’re not creating instances - we have interfaces at our disposal, and their benefit comes from not generating any source code, yet allowing us to somewhat “virtually” type-check our code.\n\n🔗 **Source:** [toddmotto.com](https://toddmotto.com/classes-vs-interfaces-in-typescript)\n\n\n### Q13: What is the difference between Classes and Interfaces in Typescript? ⭐⭐\n\n**Answer:**\n\nWe use classes as object factories. A class defines a blueprint of what an object should look like and act like and then implements that blueprint by initialising class properties and defining methods. Classes are present throughout all the phases of our code.\n\nUnlike classes, an interface is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes. Once code is transpiled to its target language, it will be stripped from interfaces.\n\nA class may define a factory or a singleton by providing initialisation to its properties and implementation to its methods, an interface is simply a structural contract that defines what the properties of an object should have as a name and as a type.\n\n🔗 **Source:** [toddmotto.com](https://toddmotto.com/classes-vs-interfaces-in-typescript)\n\n\n### Q14: What is \"Decorators\" in TypeScript? ⭐⭐\n\n**Answer:**\n\nA *Decorator* is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators are functions that take their target as the argument. With decorators we can run arbitrary code around the target execution or even entirely replace the target with a new definition.\n\nThere are 4 things we can decorate in ECMAScript2016 (and Typescript): constructors, methods, properties and parameters. \n\n🔗 **Source:** [www.sparkbit.pl](https://www.sparkbit.pl/typescript-decorators/)\n\n\n### Q15: What is getters/setters in TypeScript? ⭐⭐\n\n**Answer:**\n\nTypeScript supports **getters/setters** as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object.\n\n```js\nclass foo {\n  private _bar:boolean = false;\n\n  get bar():boolean {\n    return this._bar;\n  }\n  set bar(theBar:boolean) {\n    this._bar = theBar;\n  }\n}\n\nvar myBar = myFoo.bar;  // correct (get)\nmyFoo.bar = true;  // correct (set)\n```\n\n🔗 **Source:** [typescriptlang.org](http://www.typescriptlang.org/docs/handbook/classes.html)\n\n\n### Q16: How could you check null and undefined in TypeScript? ⭐⭐\n\n**Answer:**\n\nJust use:\n```js\nif (value) {\n}\n```\nIt will evaluate to `true` if `value` is not:\n\n* `null`\n* `undefined`\n* `NaN`\n* empty string `''`\n* `0`\n* `false`\n\nTypesScript includes JavaScript rules.\n\n\n🔗 **Source:** [stackoverflow.com](https://stackoverflow.com/questions/28975896/is-there-a-dedicated-function-to-check-null-and-undefined-in-typescript)\n\n\n### Q17: How to implement class constants in TypeScript? ⭐⭐\n\n**Answer:**\n\nIn TypeScript, the `const` keyword cannot be used to declare class properties. Doing so causes the compiler to an error with \"A class member cannot have the 'const' keyword.\" TypeScript 2.0 has the `readonly` modifier:\n\n```js\nclass MyClass {\n    readonly myReadonlyProperty = 1;\n\n    myMethod() {\n        console.log(this.myReadonlyProperty);\n    }\n}\n\nnew MyClass().myReadonlyProperty = 5; // error, readonly\n```\n\n🔗 **Source:** [stackoverflow.com](https://stackoverflow.com/questions/37265275/how-to-implement-class-constants-in-typescript)\n\n\n### Q18: Could we use TypeScript on backend and how? ⭐⭐\n\n**Answer:**\n\nTypescript doesn’t only work for browser or frontend code, you can also choose to write your backend applications. For example you could choose Node.js and have some additional type safety and the other abstraction that the language brings.\n\n1. Install the default Typescript compiler  \n\n```sh\nnpm i -g typescript\n```\n2. The TypeScript compiler takes options in the shape of a tsconfig.json file that determines where to put built files and in general is pretty similar to a babel or webpack config.\n\n```sh\n{\n  \"compilerOptions\": {\n    \"target\": \"es5\",\n    \"module\": \"commonjs\",\n    \"declaration\": true,\n    \"outDir\": \"build\"\n  }\n}\n```\n3. Compile ts files\n\n```sh\ntsc\n```\n4. Run\n\n```js\nnode build/index.js\n```\n\n🔗 **Source:** [jonathanmh.com](https://jonathanmh.com/typescript-node-js-tutorial-backend-beginner/)\n\n\n### Q19: Does TypeScript support all object oriented principles? ⭐⭐\n\n**Answer:**\n\nThe answer is **YES**. There are 4 main principles to Object Oriented Programming: \n\n* Encapsulation, \n* Inheritance, \n* Abstraction, and \n* Polymorphism. \n\nTypeScript can implement all four of them with its smaller and cleaner syntax.\n\n🔗 **Source:** [jonathanmh.com](https://jonathanmh.com/typescript-node-js-tutorial-backend-beginner/)\n\n\n### Q20: Which object oriented terms are supported by TypeScript? ⭐⭐\n\n**Answer:**\n\nTypeScript supports following object oriented terms:\n\n*   Modules\n*   Classes\n*   Interfaces\n*   Data Types\n*   Member functions\n\n🔗 **Source:** [http://www.talkingdotnet.com](http://www.talkingdotnet.com/typescript-interview-questions/)\n\n\n### Q21: What is a TypeScript Map file? ⭐⭐\n\n**Answer:**\n\n`.map` files are source map files that let tools map between the emitted JavaScript code and the TypeScript source files that created it. Many debuggers (e.g. Visual Studio or Chrome's dev tools) can consume these files so you can debug the TypeScript file instead of the JavaScript file.\n\n🔗 **Source:** [stackoverflow.com](https://stackoverflow.com/questions/17493738/what-is-a-typescript-map-file)\n\n\n### Q22: Explain how and why we could use property decorators in TS? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q23: Are strongly-typed functions as parameters possible in TypeScript? ⭐⭐⭐\n\n**Questions Details:**\n\nConsider the code:\n\n```js\nclass Foo {\n    save(callback: Function) : void {\n        //Do the save\n        var result : number = 42; //We get a number from the save operation\n        //Can I at compile-time ensure the callback accepts a single parameter of type number somehow?\n        callback(result);\n    }\n}\n\nvar foo = new Foo();\nvar callback = (result: string) : void =\u003e {\n    alert(result);\n}\nfoo.save(callback);\n```\nCan you make the result parameter in `save` a type-safe function? Rewrite the code to demonstrate.\n\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q24: Is that TypeScript code valid? Explain why. ⭐⭐⭐\n\n**Questions Details:**\n\nConsider:\n```js\nclass Point {\n    x: number;\n    y: number;\n}\n\ninterface Point3d extends Point {\n    z: number;\n}\n\nlet point3d: Point3d = {x: 1, y: 2, z: 3};\n```\n\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q25: What are different components of TypeScript? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q26: How TypeScript is optionally statically typed language? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q27: What is the default access modifier for members of a class in TypeScript? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q28: How can you allow classes defined in a module to accessible outside of the module? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q29: What's wrong with that code? ⭐⭐⭐\n\n**Questions Details:**\n\n```js\n// something is wrong\nfunction reverse(s: String): String;\n```\n\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q30: Does TypeScript supports function overloading? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q31: How To Use external plain JavaScript Libraries in TypeScript? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q32: What is Typings in Typescript? ⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q33: What is the difference between \"interface vs type\" statements? ⭐⭐⭐⭐\n\n**Questions Details:**\n\n```js\ninterface X {\n    a: number\n    b: string\n}\n\ntype X = {\n    a: number\n    b: string\n};\n```\n\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q34: How would you overload a class constructor in TypeScript? ⭐⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q35: Explain why that code is marked as WRONG? ⭐⭐⭐⭐\n\n**Questions Details:**\n\n```js\n/* WRONG */\ninterface Fetcher {\n    getObject(done: (data: any, elapsedTime?: number) =\u003e void): void;\n}\n```\n\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q36: What is one thing you would change about TypeScript? ⭐⭐⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q37: Explain when to use \"declare\" keyword in TypeScript ⭐⭐⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q38: What are Ambients in TypeScripts and when to use them? ⭐⭐⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n### Q39: Is it possible to generate TypeScript declaration files from JS library? ⭐⭐⭐⭐⭐\n\n See 👉 **[Answer](https://www.fullstack.cafe/TypeScript)**\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faershov24%2Ftypescript-interview-questions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faershov24%2Ftypescript-interview-questions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faershov24%2Ftypescript-interview-questions/lists"}