When working with Javascript, creating a class like the one below allows us to avoid declaring and initializing a property named logs
outside of the constructor:
class Logger {
constructor() {
this.logs = [];
}
}
However, transitioning to TypeScript presents an error message:
Property 'logs' does not exist on type 'Logger'.ts(2339)
To resolve this issue, we can declare the property logs in the following manner: logs: Array<any>;
A corrected TypeScript pseudo code snippet would appear as follows:
class Logger {
logs: Array<any>;
constructor() {
this.logs = [];
}
}
This question may be common for beginners exploring TypeScript. While the solution is clear, understanding the nuances, differences, and best practices between JavaScript and TypeScript remains crucial.