Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 83b11b5e502162f1f40b1c2e16ad8dcf9bff74e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * Miscellaneous types
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Define a nominal type
 *
 * Nominal types are types that are enforced by name.  Typescript implements
 * structural subtyping (duck typing), which means that two values with the
 * same structure are considered to be compatable.  This opens the
 * opportunity for certain classes of bugs: if we're expecting a Unix
 * timestamp, but we're given a user id, it'd be nice if we could catch that
 * at compile time.
 *
 * This uses a method the TS community calls "branding".  It is abstracted
 * behind a generic.  See example uses below.  I used the name `NominalType`
 * rather than `Brand` since searching for the former provides much better
 * literature on the topic, which will hopefully help in debugging when
 * errors are inevitable encountered.
 */
type NominalType<K, T> = K & { __nominal_type__: T };


/**
 * Unix timestamp
 *
 * Number of seconds since the Unix epoch (1970-01-01 UTC).
 */
type UnixTimestamp = NominalType<number, 'UnixTimestamp'>;


/**
 * Oldschool NodeJS callback
 *
 * We should migrate to promises over time.  The purpose of this type is to
 * reduce the boilerplate of these function definitions, and to clearly
 * document that this pattern is something that used to be done frequently.
 */
type NodeCallback<T, R = void> = ( e: Error | null, result: T | null ) => R;