scrimba
Understanding Typescript In Depth
Typescript section -4 Extending Generics #31
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Typescript section -4 Extending Generics #31
AboutCommentsNotes
Typescript section -4 Extending Generics #31
Expand for more info
index.ts
run
preview
console
// read it
function genericsTypes<T, R>(list: T[], param2: R): T {
console.log(param2);
return list[0];
}
// extending genrics types
interface AnyKindOfObject {
what: string;
}

interface reuseableTypes<T extends object> {
entity: T;
}

const d: reuseableTypes<AnyKindOfObject> = { entity: {what : "wert"} };
console.log(d.entity.what); // Compile

// extends
interface ObjectWithId {
id: number;
what: string;
}

interface AnyKindOfObject {
what: string;
}

interface ReusableInterface4<T extends { id: number }> {
entity: T;
}

const e: ReusableInterface4<AnyKindOfObject> = { entity: { id: 5, what: 1 } }; // Doesn't compile
const f: ReusableInterface4<ObjectWithId> = { entity: { id: 1, what: "1" } }; // Compile
const g: ReusableInterface4<string> = { entity: "test" }; // Doesn't compile
Console
"Hello from JavaScript"
,
index.html
-4:41