Open KC900201 opened 1 week ago
Hi @RayMathew,
Is it possible to look into this issue?
cc: @Buzzpy
I'm new to TypeScript. Could you give an example of type-guarding that fixes the issue?
Hi @RayMathew,
Apologies for not getting back to you sooner. TypeScript is a superset of JavaScript that enforces static typing and variables needed to be pre-defined their object types to avoid compilation errors. With the errors found in the code detected by prettier, this could cause compilation errors when production build is undergoing.
An example of a compilation error is shown below when running npm run build
Cause of issue:
Typescript enforces static typing, hence it is possible that the type of a value that TypeScript can’t know about. The return type can be ambiguous as the code might return a null
value if it doesn't run successfully.
Suggested workaround for this issue:
We could use type assertion to specify a specific type for the code line
const modalBody = document.getElementById("modal-body") as HTMLElement;
Reminder: Because type assertions are removed at compile-time, there is no runtime checking associated with a type assertion. There won’t be an exception or null generated if the type assertion is wrong.
@RayMathew I suggest we run a thorough code check using existing code linters. You may check for eslint which is a popular linting tool for most TypeScript projects.
There is an existing eslint-plugin for Astro framework that you can look for.
If it's required, you may assign to me on this task.
References:
cc: @Buzzpy
I see. I understand that type assertion will suppress the compilation error. What if instead we had null checks in the code? example:
const modalBody = document.getElementById("modal-body");
if (modalBody) {
modalBody.innerHTML = "Some content";
}
else throw Error();
@RayMathew, apologies for not getting to you sooner. I think your solution works too. If I may ask, would you like to assign me the task?
In addition to solve the null error checking, my suggestion would be to add lint checkers as well.
@RayMathew I suggest we run a thorough code check using existing code linters. You may check for eslint which is a popular linting tool for most TypeScript projects.
There is an existing eslint-plugin for Astro framework that you can look for.
If it's required, you may assign to me on this task.
References:
cc: @Buzzpy
Agreed to the need for the linting tool as well 👍🏻 Please let us know in advance which rules you plan to add in the config file.
P.S. It's up to @Buzzpy to assign tasks, she's the repo owner.
@Buzzpy may we know your opinion?
Describe the bug
To Reproduce
Expected behaviour
git commit
to prevent errors.Screenshots