source: trip-planner-front/node_modules/type-fest/source/opaque.d.ts@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/**
2Create an opaque type, which hides its internal details from the public, and can only be created by being used explicitly.
3
4The generic type parameter can be anything. It doesn't have to be an object.
5
6[Read more about opaque types.](https://codemix.com/opaque-types-in-javascript/)
7
8There have been several discussions about adding this feature to TypeScript via the `opaque type` operator, similar to how Flow does it. Unfortunately, nothing has (yet) moved forward:
9 - [Microsoft/TypeScript#15408](https://github.com/Microsoft/TypeScript/issues/15408)
10 - [Microsoft/TypeScript#15807](https://github.com/Microsoft/TypeScript/issues/15807)
11
12@example
13```
14import {Opaque} from 'type-fest';
15
16type AccountNumber = Opaque<number, 'AccountNumber'>;
17type AccountBalance = Opaque<number, 'AccountBalance'>;
18
19// The Token parameter allows the compiler to differentiate between types, whereas "unknown" will not. For example, consider the following structures:
20type ThingOne = Opaque<string>;
21type ThingTwo = Opaque<string>;
22
23// To the compiler, these types are allowed to be cast to each other as they have the same underlying type. They are both `string & { __opaque__: unknown }`.
24// To avoid this behaviour, you would instead pass the "Token" parameter, like so.
25type NewThingOne = Opaque<string, 'ThingOne'>;
26type NewThingTwo = Opaque<string, 'ThingTwo'>;
27
28// Now they're completely separate types, so the following will fail to compile.
29function createNewThingOne (): NewThingOne {
30 // As you can see, casting from a string is still allowed. However, you may not cast NewThingOne to NewThingTwo, and vice versa.
31 return 'new thing one' as NewThingOne;
32}
33
34// This will fail to compile, as they are fundamentally different types.
35const thingTwo = createNewThingOne() as NewThingTwo;
36
37// Here's another example of opaque typing.
38function createAccountNumber(): AccountNumber {
39 return 2 as AccountNumber;
40}
41
42function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {
43 return 4 as AccountBalance;
44}
45
46// This will compile successfully.
47getMoneyForAccount(createAccountNumber());
48
49// But this won't, because it has to be explicitly passed as an `AccountNumber` type.
50getMoneyForAccount(2);
51
52// You can use opaque values like they aren't opaque too.
53const accountNumber = createAccountNumber();
54
55// This will not compile successfully.
56const newAccountNumber = accountNumber + 2;
57
58// As a side note, you can (and should) use recursive types for your opaque types to make them stronger and hopefully easier to type.
59type Person = {
60 id: Opaque<number, Person>;
61 name: string;
62};
63```
64*/
65export type Opaque<Type, Token = unknown> = Type & {readonly __opaque__: Token};
Note: See TracBrowser for help on using the repository browser.