D
D
dron1122022-03-21 10:29:04
typescript
dron112, 2022-03-21 10:29:04

What is the difference between extended and initialValue when we define a generic type?

What is the difference between the first piece of code and the second.
As I understand it, the first one sets the initialValue, and the second one extends the generic type, since the word extetnded is there. What's the Difference ? Why does the ts compiler swear at the first example?

function calc<T = {a: string, b:string}>( obj:T ):number {
   return obj.a + obj.b
}


interface Icalc {
  a: number;
  b: number;
}
function calc<T = extended Icalc>( obj:T ):number {
   return obj.a + obj.b
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question