Site icon GEEKrar

5 common issues when using typescript

Typescript is like a guardrail constructed on top of JavaScript: it helps you stay on course. Although Typescript stops you from shooting yourself in the foot, you still have some concerns. Adding a type system over JavaScript is the primary goal of Typescript. Adding a type system prevents you from shooting yourself in the foot and ensures that you do not add an integer to a string.

Annotation of the type of your variables, constants, functions, and return type is done automatically. However, you can still declare any as a type and disregard all Typescript type checking. Typescript verification is bypassed by sure developers who prefer to “move things and break fast.”

  1. When attempting to load API data, failing to perform an error check

It is a favorite pastime of many programmers to write code and then manually test it in their workstations. Once they’ve validated that their code works on their laptop, many developers consider the “happy scenario” and commit their work. However, they often fail to check that the code can handle an error.

Data loading using GraphQL (spoiler alert: it’s buggy) is the goal of the code. The useQuery function in this code sends a GraphQL query and then returns whether or not the question has loaded and the query’s contents (if the results came back).

  1. The line of code you just looked at has a fundamental flaw

It does not handle failures in any meaningful way. If the request fails or the backend is unavailable, loading is false, and the problem is not being addressed. Instead, the following code should be used to determine whether or not the request returns an error and then deal with it appropriately. 

  1. Using class components

Components in the early days of React were written as classes. A class, on the other hand, necessitated the creation of a constructor and a slew of unnecessary syntactic features. In contrast, a functional component requires only a few lines of code to implement and is much easier to test.

  1. Components that are incredibly long to write

Even though this guideline has been around for a long time, it is rarely implemented by developers when they begin coding. How often have you come across a file with ten functions, each of which has over 200 lines of code in it?? To keep React components compact and fit the code of your part on the screen, the same rule applies: it’s always a good idea. At most 100 lines, components should be refactored, coded, and divided into sub-components if necessary.

  1. Using variables and not constants

Values cannot easily be traced when they are being changed because of how variables are used. Keeping track of when and whether variable changes can be complex when the program grows in size (mainly when no test exists). Immutable values are better since they cannot be changed. When updating a value, build a new const based on the previous value instead of modifying the old value.

Fix “Object is possibly ‘undefined'” error in Typescript

Microsoft created Typescript, an object-oriented language. JavaScript is a subset of it. The fix error typescript: object is possibly ‘undefined’ may appear when you try to run the command. 

For your benefit, numerous solutions are available if you run into the issue “Object is probably ‘undefined’ in Typescript.” We’ve compiled a list of possible options for you to review and select the best one for your situation.

Suggestion 1: Optional Chaining Operators are one possible solution to the problem.

To avoid non-null or undefined, use the Non-null assertion operator symbol, this is applied to the variable.

This error can only be fixed if the program receives the correct type of value. If you want a fallback option, you can use the (||) operator in the following command:

a[k].toString().toLowerCase()

replace with :

(a[k] || ”).toString().toLowerCase() 

// Or with optional chaining 

a[k]?.toString().toLowerCase() || ”

Suggestion 2: Assign a new variable to the result.

You also have the option of storing the value in a variable and then comparing it to the previously stored value. It’s possible to use v = a[k]: null and check it, for example:

Change this command:

if (a && a[k]) { 

   return textMatch(txt.toLowerCase(), a[k].toString().toLowerCase()) ? a : null; 

}

Become:

let v = a ? a[k] : null 

if (v) { 

         return textMatch(txt.toLowerCase(), v.toString().toLowerCase()) ? a : null; 

}

Suggestion 3: Make use of check text

To utilize checktext with an optional chaining operator, first install TypeScript’s stable version (using npm install typescript), and then implement the following code:

const checkText = (k: string, a: IAsset | null) => {

return (textMatch(txt.toLowerCase(), a?.[k].toString().toLowerCase()) ? a : null);

}

Conclusion

We hope you found our post to be interesting and helpful. The following are possible solutions to the fix error typescript: object is possibly ‘undefined’. Don’t panic if you encounter this problem. An easy solution exists!

Exit mobile version