added whole content

This commit is contained in:
Fedor Katurov 2022-11-03 10:38:11 +06:00
parent 1b5df685cb
commit 8b25e0631a
70 changed files with 5962 additions and 19 deletions

View file

@ -0,0 +1,50 @@
Sometimes you want to add global variable to your `window`. That thing's called [global module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation).
Say you need to call `window.doFancyThings()`. For that you should augment global `window` interface in `*.d.ts` file:
```typescript
declare global {
interface Window {
doFancyThings: () => void;
}
}
```
This is useful for declaring global `window.ethereum` (or `window.web3`) in [blockchain](/blockchain/Common%20typescript%20examples) projects with typescript, which use wallet browser extensions.
## Augmenting existing interface
For example, you have class `Sample` without any functionality:
```typescript
// Sample.ts
export class Sample {
// nothing :-)
}
```
Then you want extend it with `doFancyThings()` method. That can be achieved with said [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation):
```typescript
// fancyThings.ts
import { Sample } from "./Sample";
declare module "./Sample" {
interface Sample {
doFancyThings: () => void;
}
}
```
Now you can call `sample.doFancyThings()` by importing both `.ts` files:
```typescript
import { Sample } from "./sample";
import "./fancyThings";
const sample = new Sample();
sample.doFancyThings(); // ok
```
This example is useful for [adding global properties to component](../Frontend/Vue/Adding%20global%20properties%20to%20component.md) in vue.js.

View file

@ -0,0 +1,38 @@
This helper generates Typescript types for i18n dictionary json
files by flattening it with period delimiter. Supports plural forms.
Used for typing [i18n.js](https://www.npmjs.com/package/i18n-js) dictionaries;
```typescript
import en from './en.json';
type TranslationPath = Flatten<typeof en>;
const t = (key: TranslationPath, options?: TranslateOptions) =>
I18nLib.t(key, options);
```
Flatten type defined here:
```typescript
// This one based on answer from StackOverflow:
// https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object
export type Flatten<T, D extends number = 5> = [D] extends [never]
? never
: T extends PluralForm // plural object
? ''
: T extends object
? { [K in keyof T]-?: Join<K, Flatten<T[K], Prev[D]>> }[keyof T]
: '';
// Fix it for you plural form
type PluralForm = Record<'one' | 'few' | 'many', string>;
type Join<K, P> = K extends string | number
? P extends string | number
? `${K}${'' extends P ? '' : '.'}${P}`
: never
: never;
type Prev = [never, 0, 1, 2, 3, 4, 5, ...Array<0>];
```

View file

@ -0,0 +1,20 @@
Useful for type checking at compile and run time:
```typescript
function isFish(pet: Fish | Bird): pet is Fish {
return (pet as Fish).swim !== undefined;
}
```
Usage:
```typescript
const pet = getSmallPet();
if (isFish(pet)) {
pet.swim();
} else {
pet.fly();
}
```