TypeScript Unexpected token, A constructor, method, accessor or property was expected
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; } }
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; } }
Those who say, must not use the “_”, for them, here is some code from TypeScript site: class Employee { private _fullName: string; get fullName(): string { return this._fullName; } this._fullName = …… } Same question on Stackoverflow, you should have a look on it, especially the answer. For the time being, if accepted, we … Read more
Here expect(new TestObject()).toThrow(); new TestObject() is evaluated first, then expect(…), then …toThrow(), in accordance with operator precedence. When new TestObject() throws, anything else doesn’t matter. This is why toThrow expects a function that is supposed to throw: expect(() => { new TestObject(); }).toThrow(); This way it can be wrapped with try..catch internally when being called. … Read more
export should be static. For conditional exports CommonJS modules and exports can be used. It should be handled with ES6 modules this way: export let Foo; if (window.Foo === undefined) { Foo = class Foo { … } } else { Foo = window.Foo; } For platform-independent solution (this may not be equal to global … Read more
I see at least one issue with your requires. monster.js first line is const miniMonster = require(“./minimonster.js”); minimonster.js first line is const monster = require(“./monster.js”); This is a problem, you can not have both files evaluate at the same time. I would not require minimonster from monster.js This may fix your issue.
Move the JSDoc for someProperty into the constructor where it is first defined: /** * SomeClass is an example class for my question. * @class * @constructor * @public */ class SomeClass { constructor () { /** * someProperty is an example property that is set to `true` * @type {boolean} * @public */ this.someProperty … Read more
I think the simplest way to check if the function is ES6 class is to check the result of .toString() method. According to the es2015 spec: The string representation must have the syntax of a FunctionDeclaration FunctionExpression, GeneratorDeclaration, GeneratorExpression, ClassDeclaration, ClassExpression, ArrowFunction, MethodDefinition, or GeneratorMethod depending upon the actual characteristics of the object So the … Read more
You can use toJSON method to customise the way your class serialises to JSON: class MyClass { constructor(property) { this.property = property } set property(prop) { // Some validation etc. this._property = prop } get property() { return this._property } toJSON() { return { property: this.property } } }
Your class has no static variables (if by static variable you mean static property). getCount returns NaN (after you call increaseCount) because Animal has no count property initially. Then increaseCount does undefined + 1 which is NaN. Instances created by new Animal have a count property initially, but Animal itself does not until you call … Read more
As with any other object you want to stringify in JS, you can use JSON.stringify: JSON.stringify(yourObject); class MyClass { constructor() { this.foo = 3 } } var myClass = new MyClass() console.log(JSON.stringify(myClass)); Also worth noting is that you can customize how stringify serializes your object by giving it a toJSON method. The value used to … Read more