Last active
February 11, 2025 05:24
-
-
Save Offirmo/e3c21432cb5600ed8672c5246903726c to your computer and use it in GitHub Desktop.
[🔷TS -- rare stuff] #TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript | |
// TODO improved enums! | |
const OPERATORS = [ '*', '/', '+', '-' ] as const | |
type Operator = typeof OPERATORS[number] | |
// reduce: need to hint when acc is not Element | |
const complex_children_count = children.reduce<number>((acc, child) => { | |
if (!child || typeof child === 'string') | |
return acc | |
return acc + 1 | |
}, 0) | |
// https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html | |
// deprecation: | |
/** @deprecated Use xyz... */ | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html | |
// @ts-expect-error | |
// @ts-ignore | |
// +++helper types | |
// https://github.com/sindresorhus/type-fest#built-in-types | |
// https://www.typescriptlang.org/docs/handbook/2/typeof-types.html | |
function f(x, y) { | |
return { x: y, y: x } | |
} | |
type R = ReturnType<typeof f> // Obtain the return type of a function type. | |
type P1 = Parameters<typeof f>[0] | |
type Node = Parameters<JSON['stringify']>[0] // JSON.stringify | |
type Keys = keyof typeof REGISTRY | |
type Values = typeof REGISTRY[keyof typeof REGISTRY] | |
// interface function | |
export interface Decorator { | |
(story: Story): Story | |
} | |
// safe switch case | |
switch(action.type) { | |
case ActionType.explore: | |
return reduceⵧexplore(state, action) | |
default: | |
// @ts-expect-error TS2339 | |
throw new Error(`reduce_action() unrecognized type "${action?.type}"!`) | |
} | |
// Symbols | |
// https://fettblog.eu/symbols-in-javascript-and-typescript/ | |
// browser/env detection | |
// @ts-ignore | |
if (typeof globalThis !== 'undefined') return globalThis | |
// Set | |
const mySet: Set<string> = new Set() | |
mySet.add('foo') | |
mySet.has(5) | |
// extend an existing interface | |
declare global { | |
interface Window { | |
_debug: WebDebugApi | |
} | |
} | |
// global var | |
// https://github.com/Microsoft/TypeScript/issues/18237 | |
module globalThis { | |
var __tunnel__ = true | undefined; | |
} | |
/////////////////////////////////////////// | |
// https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html | |
type World = "world"; | |
type Greeting = `hello ${World}`; | |
/////////////////////////////////////////// | |
// advanced types https://www.typescriptlang.org/docs/handbook/advanced-types.html | |
/* | |
First & Second | |
string | number | |
pet is Fish | |
*/ | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html | |
// type unknown | |
// https://www.typescriptlang.org/docs/handbook/utility-types.html | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html | |
// some undocumented https://github.com/piotrwitek/utility-types | |
/* | |
Pick<Type, KeyA | KeyB...> | |
Omit<Type, KeyA | KeyB..."> | |
Exclude<Type, ExcludedUnion> // Exclude from T those types that are assignable to U. | |
Extract<Type, Union> // Extract from T those types that are assignable to U. | |
NonNullable<Type> // Exclude null and undefined from T. | |
Parameters<Type> | |
Partial<Type> | |
Required<Type> | |
Readonly<T> | |
Record<K extends keyof any, T> | |
InstanceType<T> – Obtain the instance type of a constructor function type. | |
*/ | |
// https://github.com/microsoft/TypeScript/blob/master/lib/lib.es5.d.ts#L1496 | |
const curried2_set_age = (age: Parameters<set_age>[1]) => {} | |
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-rc/ | |
function assert(condition: any, msg?: string): asserts condition { | |
if (!condition) { | |
throw new AssertionError(msg) | |
} | |
} | |
function isꓽFish(pet: any): pet is Fish { | |
return (pet as Fish).swim !== undefined; | |
} | |
function assertꓽisꓽFish(pet: any): asserts pet is Fish { | |
assert(isꓽFish(pet)) | |
} | |
// NOO do NOT use TS enums, not a js feature, can't be simply stripped | |
enum Colors { | |
Red = "RED", | |
Green = "GREEN", | |
Blue = "BLUE", | |
} | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html | |
type X = object // non-primitive type | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html | |
// keyof | |
// indexed access types, also called lookup type | |
type P1 = Person["name"]; // string | |
function getProperty<T, K extends keyof T>(obj: T, key: K) { | |
return obj[key]; // Inferred type is T[K] | |
} | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html | |
// never | |
// Tagged union types | |
interface Square { | |
kind: "square"; | |
size: number; | |
} | |
interface Rectangle { | |
kind: "rectangle"; | |
width: number; | |
height: number; | |
} | |
interface Circle { | |
kind: "circle"; | |
radius: number; | |
} | |
type Shape = Square | Rectangle | Circle; | |
function area(s: Shape) { | |
// In the following switch statement, the type of s is narrowed in each case clause | |
// according to the value of the discriminant property, thus allowing the other properties | |
// of that variant to be accessed without a type assertion. | |
switch (s.kind) { | |
case "square": return s.size * s.size; | |
case "rectangle": return s.width * s.height; | |
case "circle": return Math.PI * s.radius * s.radius; | |
} | |
} | |
// Specifying the type of this for functions | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html | |
//Type parameters as constraints | |
function assign<T extends U, U>(target: T, source: U): T { | |
// this is... | |
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-6.html | |
// intersection | |
function extend<T, U>(first: T, second: U): T & U { | |
let result = <T & U> {}; | |
var abc: A & B & C; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment