Urgent.News

What's breaking now, across thousands of outlets.

Tech

Things I Wish I Knew When Starting TypeScript

When I first started using TypeScript, I thought it was basically JavaScript with types. Add a few string and number annotations, fix some red errors, and you are done. Well... not exactly. The more I used TypeScript in real frontend projects, the more I realized that the difficult part was not learning the syntax. The difficult part was learning how to think about types. After working with…

When beginning to use TypeScript, one might mistakenly believe it's merely JavaScript with type annotations. However, the true challenge lies in mastering the thinking behind types. Here are some valuable insights I wish I had known when starting TypeScript:

1. Not everything requires explicit typing. I initially tried annotating every variable, but TypeScript can often infer types on its own. For instance, instead of writing const name: string = 'John';, you can simply use const name = 'John';. This approach emphasizes writing code that provides useful information through types, rather than simply adding as many type annotations as possible.

2. The 'any' type should be used sparingly. When TypeScript flags an error, the instinct might be to resort to any as a solution. However, this approach negates many of TypeScript's benefits. For example, instead of const user: any = data;, it's better to describe the expected type more precisely: type User = { name: string; age: number; }; function getUser(user: User) { return user.name; }. This allows TypeScript to assist in ensuring the correct usage of the variable.

3. Types serve as documentation. A well-defined type can explain code without the need for comments. For instance, comparing function updateUser(user: any, options: any) { // ... } to type User = { id: string; name: string; email: string; }; type UpdateUserOptions = { name?: string; email?: string; }; function updateUser(user: User, options: UpdateUserOptions) { // ... } demonstrates how types become part of the API for your code, aiding comprehension and facilitating teamwork.

4. Understand types and interfaces, but avoid overcomplicating them. Initially, I spent excessive time deciding between using type or interface in TypeScript. The key is to learn both and understand their differences. For most frontend scenarios, either option works well. For example, both type User = { id: string; name: string; }; and interface User { id: string; name: string; } describe an object with the same structure.

While there are subtle differences, particularly around declaration merging and extending types, you do not need to memorize every detail before starting a project. Begin coding and you will quickly discover when the differences matter.

5. Unions are a powerful feature. One of my favorite aspects of TypeScript is the ability to define union types. For example, type Status = 'loading' | 'success' | 'error'; allows you to assign 'loading' to a variable of type Status. This becomes particularly useful in frontend applications, such as defining RequestState = | { status: 'loading' } | { status: 'success'; data: User[] } | { status: 'error'; message: string }; This enables more precise handling of different states and data structures within your application.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Kernel & Embedded News: slab_tiny Targets 7.4; inotify Side Channel

Linux 7.4 is set to gain a slab_tiny boot option that replaces the compile-time CONFIG_SLUB_TINY, letting one kernel image serve both small-memory and full-size boards, alongside a sheaves refill…

  • slabtiny boot option introduced in Linux 7.4
  • inotify system vulnerability patched in longterm series
  • Linux 7.3 reaches release candidate 5 stage

Drift Detection Is Solved. Drift Remediation Is Not.

Your Terraform plan told you the security group changed. It has been telling you for eleven days. Nobody has reverted it, and nobody can tell you if reverting it is safe.

  • Terraform plans and Kubernetes diff checks solve drift detection
  • Remediation remains manual, risky process
  • Detection layer highly effective, but remediation gap exists

how i track funnels without cookies

i'm om yaduvanshi. i'm 18, an applied ai developer building cloudline , a cookieless google analytics alternative . one script tag, under 2 KB , no cookies, no stored ips.

  • 18-year-old AI developer creates Cloudline, cookieless Google Analytics alternative
  • Funnel tracking tracks pageviews and button clicks without personal identities
  • Users define funnel steps explicitly; Cloudline shows step counts, not individual paths

More from Tuesday 29 September →