Skip to content

Instantly share code, notes, and snippets.

@none23
Created July 2, 2020 12:33
Show Gist options
  • Save none23/c5b1950a21bc74a7fe78f7075182c0d4 to your computer and use it in GitHub Desktop.
Save none23/c5b1950a21bc74a7fe78f7075182c0d4 to your computer and use it in GitHub Desktop.
/**
* Global declarations (these can be used without importing)
*/
/**
* Номинальные типы "для бедных".
* Например `type Foo = `Nominal<'Foo', string>` - подтип строки, который:
* - может быть использован, кок строка (можно передать `let x: Foo` в функцию `function(arg:string){...}`)
* - не принимает произвольную строку (нельзя передать `let x: string` в функцию `function(arg:Foo){...}`)
* - не совместим с `type Bar = Nominal<'Bar', string>`
* Важно:
* - поле __TYPE__ - это хак, позволяющий достичь такого поведения. На самом деле никакого поля `__TYPE_` нет
*/
declare type Nominal<Name extends string, Supertype> = Supertype & { readonly __TYPE__: Name };
/** Full URL (e.g. 'https://example.com/logo.png') */
declare type URLLink = Nominal<'URLLink', string>;
/** Readable string, that can be used in a URL path and must match /^[a-z](-[a-z])*$/
* (e.g. '123', 'product/123') */
declare type URLSlug = Nominal<'URLSlug', string>;
/** Number as string (e.g. '2020', '1.23') */
declare type NumberAsString = Nominal<'NumberAsString', string>;
/** Date in ISO-8601 format (YYYY-MM-DD) (e.g. '2020-12-31') */
declare type ISODateString = Nominal<'ISODateString', string>;
/** Timestamp in ISO-8601 format (YYYY-MM-DDThh:mm:ss[.sss][Z])
* (e.g. '2020-12-31T22:34:56.000Z', '1984-01-01T00:00:00', '2000-12-01T10:00:00+03') */
declare type ISODateTime = Nominal<'ISODateString', string>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment