type-fest
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -21,1 +21,2 @@ // Basic | ||
export {PackageJson} from './source/package-json'; | ||
export {TsConfigJson} from './source/tsconfig-json'; |
{ | ||
"name": "type-fest", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "A collection of essential TypeScript types", | ||
@@ -5,0 +5,0 @@ "license": "(MIT OR CC0-1.0)", |
@@ -81,2 +81,3 @@ <div align="center"> | ||
- [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). | ||
- [`TsConfigJson`](source/tsconfig-json.d.ts) - Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7). | ||
@@ -83,0 +84,0 @@ ## Declined types |
@@ -16,5 +16,24 @@ /** | ||
type AccountNumber = Opaque<number>; | ||
type AccountBalance = Opaque<number>; | ||
type AccountNumber = Opaque<number, 'AccountNumber'>; | ||
type AccountBalance = Opaque<number, 'AccountBalance'>; | ||
// The Token parameter allows the compiler to differentiate between types, whereas "unknown" will not. For example, consider the following structures: | ||
type ThingOne = Opaque<string>; | ||
type ThingTwo = Opaque<string>; | ||
// 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 }`. | ||
// To avoid this behaviour, you would instead pass the "Token" parameter, like so. | ||
type NewThingOne = Opaque<string, 'ThingOne'>; | ||
type NewThingTwo = Opaque<string, 'ThingTwo'>; | ||
// Now they're completely separate types, so the following will fail to compile. | ||
function createNewThingOne (): NewThingOne { | ||
// As you can see, casting from a string is still allowed. However, you may not cast NewThingOne to NewThingTwo, and vice versa. | ||
return 'new thing one' as NewThingOne; | ||
} | ||
// This will fail to compile, as they are fundamentally different types. | ||
const thingTwo = createNewThingOne() as NewThingTwo; | ||
// Here's another example of opaque typing. | ||
function createAccountNumber(): AccountNumber { | ||
@@ -37,6 +56,12 @@ return 2 as AccountNumber; | ||
// This will compile successfully. | ||
accountNumber + 2; | ||
// This will not compile successfully. | ||
const newAccountNumber = accountNumber + 2; | ||
// As a side note, you can (and should) use recursive types for your opaque types to make them stronger and hopefully easier to type. | ||
type Person = { | ||
id: Opaque<number, Person>; | ||
name: string; | ||
}; | ||
``` | ||
*/ | ||
export type Opaque<Type> = Type & {readonly __opaque__: unique symbol}; | ||
export type Opaque<Type, Token = unknown> = Type & {readonly __opaque__: Token}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77464
20
1597
633