Typescript Flashcards
What is Typescript?
TypeScript is an open-source language developed by Anders Hejlsberg at Microsoft. It’s a statically-typed superset of JavaScript that compiles to plain JavaScript. It runs on any browser, host, and operating system. That means all valid JavaScript code is also TypeScript code. It offers advanced features such as IntelliSense, code completion, safe refactorings, etc.
Why TypeScript?
As JavaScript projects grow in size, they become difficult to maintain. There are a few reasons for this. First, JavaScript was never designed to build large-scale applications. Its original purpose was to provide small scripting functionality for a web page. Until recently, it didn’t provide tools and constructs for structuring large projects, such as classes, modules, and interfaces. Also, JavaScript is dynamically typed. It doesn’t support features such as IntelliSense.
TypeScript adds optional static typing and language features such as classes and modules. It’s important to know that all these advanced features add zero cost to JavaScript. A typeScript is purely a compile-time tool. Once you compile, you are left with plain, idiomatic JavaScript. TypeScript is a language for application scale JavaScript development.
What is any type, and when to use it?
There are times when you want to store a value in a variable but don’t know the type of that variable in advance. For example, the value is coming from an API call or the user input. The ‘any’ type allows you to assign a value of any type to the variable of type any.
TypeScript assumes a variable is of type any when you don’t explicitly provide the type, and the compiler cannot infer the type from the surrounding context.
What is Typescript?
Superset of JavaScript: TypeScript is a programming language that is a strict syntactical superset of JavaScript, adding optional static typing to the language.
Static Typing: TypeScript allows developers to define types for variables, function parameters, and function return values, enabling better type checking at compile time. Class Features: TypeScript supports features from ECMAScript 6 (ES6) and beyond, such as classes, interfaces, and decorators, making it easier to write object-oriented code. Transpilation: TypeScript code is transpiled to plain JavaScript, making it compatible with any browser or JavaScript runtime. Tooling: TypeScript provides powerful development tools, including autocompletion, type inference, and refactoring support, improving developer productivity.
When to use typescript?
Large Codebases: TypeScript’s static typing and tooling are beneficial for large codebases, making it easier to manage and refactor code.
Complex Applications: TypeScript's features like classes, interfaces, and namespaces are useful for building complex, object-oriented applications. Team Collaboration: TypeScript's type annotations and documentation features can improve code readability and collaboration among team members. Type Safety: Use TypeScript when type safety is crucial for your application, as it can help catch type-related errors at compile time rather than runtime. Modern Web Development: TypeScript is a popular choice for modern web development frameworks like Angular, React, and Vue, providing better development and debugging experiences.
What is an unknown type, and when to use it in TypeScript?
The unknown type is the type-safe counterpart of any type. You can assign anything to the unknown, but the unknown isn’t assignable to anything but itself and any, without performing a type assertion of a control-flow-based narrowing. You cannot perform any operations on a variable of an unknown type without first asserting or narrowing it to a more specific type.
What are the primitive types in TypeScript?
TypeScript has three primitive types that are frequently used: string, number, and boolean. These correspond to the similarly named types in JavaScript.
string: represents text values such as “javascript”, “typescript”, etc. number: represents numeric values like 1, 2, 32, 43, etc. boolean: represents a variable that can have either a ‘true’ or ‘false’ value.
Provide the syntax of a function with the type annotations.
Functions are blocks of code to perform a specific code. Functions can optionally take one or more arguments, process them, and optionally return a value.
How to specify optional properties in TypeScript?
An object type can have zero or more optional properties by adding a ‘?’ after the property name.
let pt: { x: number; y: number; z?: number } = {
x: 10,
y: 20
};
console.log(pt);
In the example above, because the property ‘z’ is marked as optional, the compiler won’t complain if we don’t provide it during the initialization.
Explain the concept of null and its use in TypeScript.
In programming, a null value indicates an absence of value. A null variable doesn’t point to any object. Hence you cannot access any properties on the variable or call a method on it.
In TypeScript, the null value is indicated by the ‘null’ keyword. You can check if a value is null as follows:
What is the typeof operator? How is it used in TypeScript?
Similar to JavaScript, the typeof operator in TypeScript returns the type of the operand as a string.
Provide the syntax for optional parameters in TypeScript.
A function can mark one or more of its parameters as optional by suffixing its name with ‘?’. In the example below, the parameter greeting is marked optional.
Explain how optional chaining works in TypeScript.
Optional chaining allows you to access properties and call methods on them in a chain-like fashion. You can do this using the ‘?.’ operator.
TypeScript immediately stops running some expression if it runs into a ‘null’ or ‘undefined’ value and returns ‘undefined’ for the entire expression chain.
Using optional chaining, the following expression
let x = foo === null || foo === undefined ? undefined : foo.bar.baz();
can be expressed as:
let x = foo?.bar.baz();