CODEREVIEW - Typescript noob
in javascript i would write this by first defining the empty object and then filling it up, so as to keep my code DRY
typescript doesn't seem to like this... is there a best practice?
2 Replies
From ChatGPT:
In TypeScript, it is best practice to use a more explicit approach for initializing variables, especially when working with interfaces or types. Instead of initializing all properties as null, you can use a technique called "object destructuring with default values" to initialize the properties with their appropriate types and default values.
An example of this technique would be:
This way, you can ensure that the properties of the object will always have the correct type and default value.
Alternatively, you could use a method like Object.assign() to copy over the properties and default values, but this approach is not as readable and might be harder to maintain.
ME >> i woudl check if there's a runtime error first bbefore running the getters
CHATGPT:
Yes, checking for runtime errors first before running the getters is a good idea. This way, you can avoid running the getters and unnecessary processing if there is a runtime error. Here is an example of how you could check for runtime errors before running the getters:
This way, if there is a runtime error, the function will return the error object and skip running the other getters.
its a little wrong, but amazing
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View