HOWPROGRAMMINGWORKS Telegram 1683
⭐️ Good and bad cases for TypeScript union types based on JavaScript V8 optimizations

👍 Good cases for union types:
- Union of strings instead of enum:
type Direction = 'north' | 'south' | 'east' | 'west';
- Union of numeric as status or result code: type
StatusCode = 200 | 201 | 204 | 400 | 500;
- Union with shared properties:
type MailTarget = User | Company; (both with email)
- Union with common method:
type Thenable = Promise | Query; (both with then method)

👎 Bad cases for union types:

- Polymorphic object shapes causing depots:
type Something = User | Socket | string;
- Requiring extensive "if"-logic and type checking:
type Input = string | number | boolean;
- Inconsistent return types:
function getData(id: number): string | string[];
- Mixed primitives and objects:
type Value = number | { value: number };
- Сontradictory members:
type Person = { name: string; } | { name: number[] };
- Union types that include any:
type FlexibleType = number | any;
- Incompatible contracts:
type Handler = (() => string) | ((event: Event, data: any) => void);

🎁 Empty value for primitive types and reference types:
- Use null for empty reference types: Object, Function, Array, etc...
- Use undefined for empty primitive types: string, number, boolean, bigint
- Avoid mixing symbols with other types in unions



tgoop.com/HowProgrammingWorks/1683
Create:
Last Update:

⭐️ Good and bad cases for TypeScript union types based on JavaScript V8 optimizations

👍 Good cases for union types:
- Union of strings instead of enum:
type Direction = 'north' | 'south' | 'east' | 'west';
- Union of numeric as status or result code: type
StatusCode = 200 | 201 | 204 | 400 | 500;
- Union with shared properties:
type MailTarget = User | Company; (both with email)
- Union with common method:
type Thenable = Promise | Query; (both with then method)

👎 Bad cases for union types:

- Polymorphic object shapes causing depots:
type Something = User | Socket | string;
- Requiring extensive "if"-logic and type checking:
type Input = string | number | boolean;
- Inconsistent return types:
function getData(id: number): string | string[];
- Mixed primitives and objects:
type Value = number | { value: number };
- Сontradictory members:
type Person = { name: string; } | { name: number[] };
- Union types that include any:
type FlexibleType = number | any;
- Incompatible contracts:
type Handler = (() => string) | ((event: Event, data: any) => void);

🎁 Empty value for primitive types and reference types:
- Use null for empty reference types: Object, Function, Array, etc...
- Use undefined for empty primitive types: string, number, boolean, bigint
- Avoid mixing symbols with other types in unions

BY HowProgrammingWorks - JavaScript and Node.js Programming


Share with your friend now:
tgoop.com/HowProgrammingWorks/1683

View MORE
Open in Telegram


Telegram News

Date: |

Unlimited number of subscribers per channel On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." The court said the defendant had also incited people to commit public nuisance, with messages calling on them to take part in rallies and demonstrations including at Hong Kong International Airport, to block roads and to paralyse the public transportation system. Various forms of protest promoted on the messaging platform included general strikes, lunchtime protests and silent sit-ins. A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP. Joined by Telegram's representative in Brazil, Alan Campos, Perekopsky noted the platform was unable to cater to some of the TSE requests due to the company's operational setup. But Perekopsky added that these requests could be studied for future implementation.
from us


Telegram HowProgrammingWorks - JavaScript and Node.js Programming
FROM American