misc-utils-of-mine-typescript
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

Utilities for dealing / defining types in TypeScript.

API

API

Examples:

Example with array

Check given array length

type Manager = 'John'|'Chris'|'Laura'
type Developer = 'Sebastian'|'Andrew'|'Gabriel'
type Name = Manager|Developer
 
interface Person<N extends Name> {
  name: N
  age?: number
}
 
// we want to check that developers in a team has exactly the number they declare at compile time
interface Team<DeveloperCount extends number> {
  manager: Person<Manager>
  developers: ArrayLiteral<Person<Developer>, DeveloperCount>
 }
 
 // strictly check that given developers length corresponds to team's declaration
declare function setDevelopers<DeveloperCount extends number>(team: Team<DeveloperCount>, developers: ArrayLiteral<Person<Developer>, DeveloperCount>):void
 
declare var team2: Team<2>
declare var team1: Team<1>
 
// OK:
setDevelopers(team2, [{name: 'Sebastian'}, {name: 'Andrew'}])
setDevelopers(team1, [{name: 'Sebastian'}])
 
// fail to compile:
setDevelopers(team2, []) 
setDevelopers(team1, [{name: 'Sebastian'}, {name: 'Andrew'}])

Package Sidebar

Install

npm i misc-utils-of-mine-typescript

Weekly Downloads

12

Version

0.0.12

License

MIT

Unpacked Size

68 kB

Total Files

39

Last publish

Collaborators

  • cancerberosgx