The Basics Flashcards
Declare a let called gen for a generic type called GenericType that takes 2 Specific types: SpecificType1 and SpecificType2
let gen : GenericType<SpecificType1, SpecificType2, ...>;
Create a let called coordinates and assign it to an array of coordinate using this type:
type Coordinate = [number, number];
let coordinates: Array<Coordinate>;
Will this yield an error:
type Coordinate = [number, number]; let coordinates: Array<Coordinate>; coordinates = [ [30, 100, 0], [100, 50], ];
Yes, because each coordinate can only have 2 dimensions.
Create a let called result that is a generic promise to return something of type Response.
If you don’t know the Type Response see:
https://developer.mozilla.org/en-US/docs/Web/API/Response
let result: Promise<Response>;
This is useful when fetching information from api like so:
let promisedResponse: Promise<Response> = fetch("https://myapi.dev/api/");
How can we use the result of this to output if the response was OK?
const promisedResponse: Promise<Response> = fetch("https://swapi.dev/api/");
promisedResponse.then((res) => console.log(res.ok));
How can we narrow the type of the record keys so that only ‘rodj’ or ‘janes’ are accepted?
type Result = { firstName: string; surname: string; score: number; }; type ResultRecord = Record<string, Result>; const records: ResultRecord = { rodj: { firstName: "Rod", surname: "James", score: 70, }, janes: { firstName: "Jane", surname: "Smith", score: 95, } };
By changing the ResultRecord type definition from:
type ResultRecord = Record<string, Result>;
to:
type ResultRecord = Record<"rodj"|"janes", Result>;
Make this function generic:
function firstOrNull(array: string[]): string | null { return array.length === 0 ? null : array[0]; }
function firstOrNull<T>(array: T[]): T | null { return array.length === 0 ? null : array[0]; }
Declare an array and bind it to a variable called myArray for the class Product without using:
new Array aka Array<Product>:
class Product { name: string; desc: string; meta: string }
let myArray : Product[];
Note: if you use const you have to initialize the array like so:
const myArray: Product[] = [];
Can interfaces be generic and accept multiples types ?
Yes. A common use if for forms.
Create a typed const contactForm using these 2 interfaces and assign a first value of “bob” and “bob@someemail.com” to contactForm:
interface Form<T> { values: T; } interface Contact { name: string; email: string; }
const contactForm: Form<Contact> = { values: { name: "Bob", email: "bob@someemail.com" } }
Explain this interface and what the K in keyof T is…
interface Form<T> { values: T; errors: { [K in keyof T]?: string; }; }
- The type is in curly brackets, so we are constructing an object type.
- [K in keyof T] will put all the keys in the type T into a string literal union. This will be “name” | “email” for contactForm.
- [K in keyof T] is the property name of the object being constructed (T).
- The ? after the property name means the properties are optional.
- The type for the properties is string.
So given a T that would be an interface:
interface Contact { name: string; email: string; }
The type for the errors is:
{name?: string; email?: string}.
Is it possible to pass types into a type alias ?
Yes. Using this syntax:
type TypeName<T1, T2, ...> = { ... }
Implement a generic Form type with 2 properties:
values for our generic T
and an
optional errors for each key of T
type Form<T> = { values: T; errors: { [K in keyof T]?: string }; };
Can we declare generic classes ?
Yes
What is the syntax to create a generic class using T1,T2,T3..
class ClassName<T1, T2, …> {
…
}
Create a generic class called List that will take a single generic type T and keep the items in a private array of T called items.
class List<T> { private items: T[] = []; }
Add a add method on this class to add an item of type T.
This new add method needs to push a provided item of type T in private Items.
class List<T> { private items: T[] = []; }
class List<T> { private items: T[] = []; add(item: T){ this.items.push(item); } }
Can generic parameter have a default value?
Yes