TypeScript Flashcards
What are unions in TS?
Unions in TypeScript refer to a type annotation that allows a variable or parameter to have multiple possible types. A union type is denoted by using the vertical bar (|) between two or more types, indicating that a value can have any of those types.
function printValue(value: number | string) {
console.log(value);
}
printValue(3)
printvalue(“three”)
What are enums in TS?
Enums in TypeScript are a way to define a set of named values, where each value has a corresponding numeric or string value. Enums allow for the creation of a set of predefined constants with meaningful names, making code more readable and maintainable. Enums provide a way to represent a group of related values as a single named type in TypeScript.
enum Direction {
Up,
Down,
Left,
Right
}
let myDirection: Direction = Direction.Up;
console.log(myDirection); // prints 0
What are tuples in TS?
Tuples in TypeScript are a special type of array that allows for specifying the type and order of elements in the array. We can have fixed number of elements, where each element has a specific types.
What does type annotation mean in TS?
Type annotation in TypeScript refers to explicitly specifying the type of a variable, function parameter, or function return value using TypeScript’s type system. It allows you to explicitly define the expected type of a value or expression, which TypeScript will use to provide type checking and better static type inference during compilation.
Type annotations in TypeScript use a colon (:) followed by the type declaration to specify the type of a value. For example:
let myVar: number = 42; // Type annotation for a number variable
let myString: string = “Hello”; // Type annotation for a string variable
let myBool: boolean = true; // Type annotation for a boolean variable
or
function add(a: number, b: number): number { // Type annotations for function parameters and return value
return a + b;
}
What is interface?
In TypeScript, an interface is a way to define the structure or shape of an object. It describes the properties, methods, and types of values that an object must have in order to meet the requirements of the interface ( similar to type …)
interface Person {
name: string;
age: number;
address?: string; // Optional property
}
function greet(person: Person): void {
console.log(Hello, ${person.name}! You are ${person.age} years old.
);
}
const john: Person = {
name: ‘John’,
age: 30
};
greet(john);
What does mean Strongly Type Language?
STL means that language demands specification of data types. TS is a statically typed language. This means types are checked at compile time. JS is dynamically typed language. This means typed are checked at a runtime. JS is loosely typed language aka “weakly typed”
What does infer or implicit mean?
If we don’t write data explicitly, TypeScript is going to infer type.
What is literal type in TS?
let user = ‘Max’ | ‘Arek’
What does void do in TypeScript?
Void type is used to indicate that a function does not return any value. It’s essentially the opposite of the any type, which can be anything.
What does type aliases refer to in TypeScript?
Type aliases are used to create new names for existing types. For example, if you have a union type string | number, you can create an alias for it:
type alphanumeric = string | number;
What is TypeScript?
TypeScript is statically typed language. Types are checked at the compile time.
What does type do in TypeScript?
type is a keyword used to define custom types. It allows you to create a new name for a type or a combination of types. You can use it to create complex types using unions, intersections, and tuples.
Differences between type and interface
Interfaces: Generally preferred for public API’s definition when authoring a library or 3rd party ambient type definitions due to their extensibility. Interface support “extends” keyword.
Types: Preferred for complex type transformations or when using types that cannot be expressed as an interface, such as unions or tuples.
What is type assertion?
type assertions - type of a value that TS can’t infer on its own. type One = string, and let d<One>"Max".
<> angle brackets are not usable in React file
example:
let someValue: any = "this is a string";</One>
What is index singature in TypeScript?
Is used in an interface or type to specify the types of possible values that can be used to index into an object,as well as the types of values that those indices can return. Useful when you when you wan tto declare a type that can have properties with names that are not known at design time.
interface StringArray {
[index: number] : string;
}