Buzzpy / Dev-Encyclopedia

An encyclopedia for everything, Programming.
https://devpedia.dev
Other
262 stars 58 forks source link

Bug (Minor) - Linting error in source code #148

Open KC900201 opened 1 week ago

KC900201 commented 1 week ago

Issue Banner Image

Describe the bug

To Reproduce

Expected behaviour

Screenshots

image

image

KC900201 commented 5 days ago

Hi @RayMathew,

Is it possible to look into this issue?

cc: @Buzzpy

RayMathew commented 5 days ago

I'm new to TypeScript. Could you give an example of type-guarding that fixes the issue?

KC900201 commented 3 days ago

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

image

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.

image

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.

KC900201 commented 3 days ago

@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

RayMathew commented 3 days ago

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();
KC900201 commented 21 hours ago

@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

RayMathew commented 6 hours ago

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.

KC900201 commented 6 hours ago

@Buzzpy may we know your opinion?