Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  28] [ 4]  / answers: 1 / hits: 104722  / 7 Years ago, tue, march 28, 2017, 12:00:00

Just trying to write a function within a class using typescript.



class Test 
{
function add(x: number, y: number): number {
return x + y;
}
}


This results in the following error:




TypeScript Unexpected token, A constructor, method, accessor or
property was expected.




I copied the example from: https://www.typescriptlang.org/docs/handbook/functions.html



Am I missing something? I'm confused!


More From » typescript

 Answers
152

You shouldn't use the function keyword in a Typescript class definition. Try this instead:



class Test { 
add(x: number, y: number): number {
return x + y;
}
}

[#58354] Sunday, March 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elliem

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;