Recently, I kicked off a fresh Next.js project using TypeScript by running
npx create-next-app --example with-typescript with-typescript-app
(https://github.com/zeit/next.js/tree/master/examples/with-typescript).
However, when attempting to define a class like this:
export class Programm {
id: string;
name: string;
thumbnailUrl: string;
}
I encountered a syntax error saying:
Property 'id' has no initializer and is not definitely assigned in the constructor.ts(2564)
The issue was resolved when I added a constructor as follows:
constructor(id: string, name: string, thumbnailUrl: string) {
this.id = id;
this.name = name;
this.thumbnailUrl = thumbnailUrl;
}
Why does this happen and how could I initialize the properties to be null while creating an object of the class?
Interestingly, the same code works fine in Angular without the need for the constructor.