function add(a:T, b:T):T{ return a + b as T } generics does not + so we need to define a subtype of T which extends number here .. thus T is a subtype of number
TypeScript doesn't allow the + operator on generic types with a union constraint. To handle addition for generic types that can support the + operator, you can use function overloading.
function add(a: T, b: T): T { if (typeof a === 'number' && typeof b === 'number') { return a + b as T; // Use 'as T' to assert the return type } else if (typeof a === 'string' && typeof b === 'string') { return (a + b) as T; // Use 'as T' to assert the return type } else { throw new Error('Unsupported types for addition'); } } const result1 = add(5, 10); const result2 = add("Hello", "world!");
It gives me error can any to know how to solve it function addFun(a:T, b:T):T{ return a+b } console.log(addFun(45, 62)) error:Operator '+' cannot be applied to types 'T' and 'T'.
yes same here it give me error , here is my code function addFun(a:T, b:T):T{ return a+b } console.log(addFun(45, 62)) error:Operator '+' cannot be applied to types 'T' and 'T'.
🚀 For Source Code & Home Assignment 👉 thapatechnical.shop/courses/typescript-crash-course
Sir I pay for react js source code ₹50 rs but I didn't get source code
Assessment:
function add(a:T,b:T):T ;
function add(a:T ,b:T):T ;
function add(a:any,b:any):any {
return a+b;
}
const result1 = add(5,10);
const result2 = add("Hello ","World!");
#HomeWork
function add(val1:T,val2:T):T{
return val1 + val2;
}
let result1 = add(24,8)
let result2 = add('hello','World')
console.log(result1)
console.log(result2)
function add(a:T, b:T):T{
return a + b as T
}
generics does not + so we need to define a subtype of T which extends number here .. thus T is a subtype of number
TypeScript doesn't allow the + operator on generic types with a union constraint. To handle addition for generic types that can support the + operator, you can use function overloading.
const add = (par1: T, par2: T): T => {
return par1 + par2;
};
const result1 = add(5, 10);
const result2 = add("Hello", "World");
Hi, what is difference between generics & any.
Bhaiya mern stack application ko azure par host kaise karna uska video please
source is not in your website for type script kindly share link for source code
Thanks sir ❤❤
day-19- your explanation is aswesome like your videos
function add(a: T, b: T): T {
if (typeof a === 'number' && typeof b === 'number') {
return a + b as T; // Use 'as T' to assert the return type
} else if (typeof a === 'string' && typeof b === 'string') {
return (a + b) as T; // Use 'as T' to assert the return type
} else {
throw new Error('Unsupported types for addition');
}
}
const result1 = add(5, 10);
const result2 = add("Hello", "world!");
where can i find source code
It gives me error can any to know how to solve it
function addFun(a:T, b:T):T{
return a+b
}
console.log(addFun(45, 62))
error:Operator '+' cannot be applied to types 'T' and 'T'.
bro i got same error, have you solved ??
Sir It's gives an error how can I take multiple parameters using generics.
yes same here it give me error , here is my code
function addFun(a:T, b:T):T{
return a+b
}
console.log(addFun(45, 62))
error:Operator '+' cannot be applied to types 'T' and 'T'.
Sir I pay for source code ₹50 rs but I didn't get source code
Op ❤❤😊
🔥
💚💜🧡
1 doubt session lelo Thapa Bhaiya Bahut doubt aa rahe hai mujhe
Sure
Only one example is not enough. It’s a single example. It won’t work
Exactly, I also had to watch out other youtuber's videos to understand it properly.
I would says its not a detailed explaination. It looks like you just wanna create this video just for the sake of quantity not quality.
👍🙏
your vdo hide many code !
that not gd to see
First visit
Congratulations 🎊
Unsubscribed because of picture in thumbnail 😐
ᴛʜᴀɴᴋ ʏᴏᴜ ꜱᴏ ᴍᴜᴄʜ ꜱɪʀ.