Skip to content

Instantly share code, notes, and snippets.

function Mixin(...classRefs: any[]) {
return merge(class {}, ...classRefs);
}
function merge(derived: any, ...classRefs: any[]) {
classRefs.forEach(classRef => {
Object.getOwnPropertyNames(classRef.prototype).forEach(name => {
if (name !== 'constructor') {
Object.defineProperty(
derived.prototype,
https://stackoverflow.com/questions/68237580/intersection-to-the-most-restricted-type/68240054#68240054
https://stackoverflow.com/questions/68207767/typescript-infer-possible-object-keys-based-on-value-of-previous-argument/68208487#68208487
https://stackoverflow.com/questions/68159882/strongly-typed-worker/68160492#68160492
https://stackoverflow.com/questions/68080294/create-and-use-type-from-the-keys-of-an-object-to-check-type-on-a-chosen-value-f/68080700#68080700
https://stackoverflow.com/questions/67820596/typescript-translation-strings-typings-solution/67821159#67821159
https://stackoverflow.com/questions/67813248/filtering-out-values-from-readonly-tuple-type/67820941#67820941
https://stackoverflow.com/questions/67787893/how-to-make-typescript-distinguish-between-a-template-literal-string-and-any-oth/67788029#67788029
https://stackoverflow.com/questions/67471328/typescript-infer-return-type-of-array-based-on-input-array-elements/67471664#67471664
https://stackoverflow.com/questions/67445170/infer-type-bas
import { bar } from "bar";
import { UnionToIntersection } from "./1_Distributive";
function overload(a: string): number;
function overload(a: number): string;
function overload(a: string | number): string | number {
return null as any;
}
const str = overload(2); // string
@captain-yossarian
captain-yossarian / validation_composition.ts
Last active July 7, 2021 11:01
Validation composition
import { IsUnion } from "./1_Distributive";
/**
* Example zero
*/
const notArray = <T>(arg: T extends Array<any> ? never : T) => arg;
const test1 = notArray(1); // ok
const test2 = notArray("string"); // ok
/**
* Map
*/
type MapPredicate<T> = [T];
type Mapped<
Arr extends Array<unknown>,
Result extends Array<unknown> = []
> = Arr extends [] // 1. end of recursion, this is the last call
? Result
/**
* 1. DISTRIBUTIVE CONDITIONAL TYPES
*/
type Distributed<T> = T extends any ? Array<T> : never;
{
// distribution work only with naked types
type Result = Distributed<number | string>;
}
// we need to make it covariant / non naked
type LinkedList<T> =
Option<{
head: Option<T>,
tail: LinkedList<T>
}>
/**
* Shamelessly stolen from Rust
*/
type None = null | undefined;
type Some<T> = T
type Option<T> = Some<T> | None
{
const obj = {
user: {
name: 'John'
}
}
const result1 = deepPickFinal(obj, 'user') // { name: string; }
const result2 = deepPickFinal(obj, 'user', 'name') // string
}

Привіт, мене звати Сергій. Працюю 4 роки як Front End програміст, здебільшого з TypeScript. Ця стаття буде присвячена Union типам. У цій статті, я спробую показати коли краще ужити Union, як правильно і не правильно з ними проацювати. Більшість прикладів, які я описуватиму - взяті із запитань на StackOverflow, тобто скоріш за все будуть корисні з практичної точки зору. Прошу вибачення наперед, що використовуватиму англійські слова, оскільки не завжди можу підібрати правильний переклад.

Unions

В багатьох випадках краще використати Union замість Enum. По-перше тільки для того, що Union не займає місця. По друге Enums мають свої недоліки.

Приклад: