Devs-Mentoring.pl - Non Null Assertion Operator
typescript
a month ago
746 B
3
Indexable
Never
// Non Null Assertion Operator // Problem function joinMenteeWithMentor(mentee: string | null, mentor: string | null) { let checkNames = () => { if (mentee === null || mentee === undefined) { mentee = ""; } if (mentor === null || mentor === undefined) { mentor = ""; } } checkNames(); return mentee.concat(mentor); // w strictMode, kompilator zaprotestuje, że mentee lub mentor mogą być null } console.log(joinMenteeWithMentor("Kacper", "Adam")); // Rozwiązanie Aby zapewnić kompilator, że jesteśmy pewni, iż mentee i mentor nie osiągną wartości null lub undefined, wystarczy zmienić linię 15. na: return mentee!.concat(mentor!);