Reputation: 47
I am new to TypeScript.Currently i am trying to learn the interface in TypeScript.Here is my code :
interface ClockInterface{
currentTime:Date;
setTime(d:Date):void;
}
class Clock implements ClockInterface{
currentTime:Date;[**[ts] Property 'currentTime' has no initializer and is not definitely assigned in the constructor.]**
setTime(d:Date){
this.currentTime =d;
}
constructor(h: number, m: number) { }
}
Upvotes: 0
Views: 2957
Reputation: 4661
Add (!) sing after name:
someField!: string;
OR
Open TypeScript config file
tsconfig.json
and add this code to compiler options
"angularCompilerOptions": {
// ...
"strictPropertyInitialization": false
// ...
}
Note: it will make static analysis weaker
Upvotes: 0
Reputation: 95
To remove this error go into the "tsconfig.json" and remove strict="true". It works for me, it checks strictnullcheck error.
Upvotes: 0
Reputation: 691625
You're in strict mode (specifically, the strictNullChecks
option is true).
So a variable of type Date
must be of type Date
. It may not be null or undefined. Since you're not initializing the currentTime
in the constructor, the variable is undefined until the setTime()
method is called. So you're violating your type definition.
Either initialize the variable in the constructor, or change the type to Date | undefined
, or change the compiler settings.
Upvotes: 1