The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an Array
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a boolean
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to a boolean
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a boolean
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to a boolean
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a Dictionary<T>
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a Dictionary<T>
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an AnyFunction
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an AnyFunction
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an instance of constructor type T
if it is type-compatible, or returns undefined
+
Narrows an unknown
value to an instance of constructor type T
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
Narrows an AnyJson
value to a JsonArray
if it is type-compatible, or returns undefined
otherwise.
Narrows an AnyJson
value to a JsonArray
if it is type-compatible, or returns the provided default otherwise.
Narrows an AnyJson
value to a JsonArray
if it is type-compatible, or returns undefined
otherwise.
Narrows an AnyJson
value to a JsonArray
if it is type-compatible, or returns the provided default otherwise.
Narrows an AnyJson
value to a JsonMap
if it is type-compatible, or returns undefined
otherwise.
Narrows an AnyJson
value to a JsonMap
if it is type-compatible, or returns the provided default otherwise.
Narrows an AnyJson
value to a JsonMap
if it is type-compatible, or returns undefined
otherwise.
Narrows an AnyJson
value to a JsonMap
if it is type-compatible, or returns the provided default otherwise.
Narrows an unknown
value to a number
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to a number
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a number
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to a number
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an object
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to an object
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a plain object
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a plain object
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to an object
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a string
if it is type-compatible, or returns undefined
otherwise.
The value to test.
-Narrows an unknown
value to a string
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
Narrows an unknown
value to a string
if it is type-compatible, or returns undefined
otherwise.
The value to test.
+Narrows an unknown
value to a string
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Asserts that a given condition
is true, or raises an error otherwise.
The condition to test.
-Optional
message: stringThe error message to use if the condition is false.
-AssertionFailedError If the assertion failed.
-Asserts that a given condition
is true, or raises an error otherwise.
The condition to test.
+Optional
message: stringThe error message to use if the condition is false.
+AssertionFailedError If the assertion failed.
+Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns undefined
otherwise.
Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns undefined
otherwise.
See also caveats noted in isAnyJson.
-The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was not a JSON value type.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was not a JSON value type.
+Narrows an unknown
value to an Array
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to an Array
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to a boolean
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to a boolean
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to a Dictionary<T>
if it is type-compatible and tests positively
+
Narrows an unknown
value to a Dictionary<T>
if it is type-compatible and tests positively
with isDictionary, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to an AnyFunction
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to an AnyFunction
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to instance of constructor type T
if it is type-compatible, or raises an error
+
Narrows an unknown
value to instance of constructor type T
if it is type-compatible, or raises an error
otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows a type Nullable<T>
to a T
or raises an error.
Narrows a type Nullable<T>
to a T
or raises an error.
Use of the type parameter T
to further narrow the type signature of the value being tested is
strongly discouraged unless you are completely confident that the value is of the necessary shape to
conform with T
. This function does nothing at either compile time or runtime to prove the value is of
@@ -6,7 +6,7 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-Optional
message: stringThe error message to use if value
is undefined
or null
.
AssertionFailedError If the value was undefined.
-AssertionFailedError If the value was undefined.
+Narrows an unknown
value to a number
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to a number
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to an object
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to an object
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to an object
if it is type-compatible and tests positively with isPlainObject,
+
Narrows an unknown
value to an object
if it is type-compatible and tests positively with isPlainObject,
or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to a string
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
-Narrows an unknown
value to a string
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
AssertionFailedError If the value was undefined.
+Narrows an unknown
value to an AnyJson
if it is type-compatible*, or returns undefined
otherwise.
Narrows an unknown
value to an AnyJson
if it is type-compatible*, or returns undefined
otherwise.
* This is not a 100% safe operation -- it will not deeply validate plain object or array structures
to ensure that they contain only AnyJson
values. When type-narrowing potential objects or arrays with this
function, it's the responsibility of the caller to understand the risks of making such a shallow type assertion
over the value
data.
The value to test.
-Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns the provided default otherwise.
The value to test.
-The default to return if value
was undefined or of the incorrect type.
The value to test.
+Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns the provided default otherwise.
The value to test.
+The default to return if value
was undefined or of the incorrect type.
Narrows an array of type T
to a JsonArray
using a shallow type-compatibility check. Use this when the source of
+
Narrows an array of type T
to a JsonArray
using a shallow type-compatibility check. Use this when the source of
the array is known to be JSON-compatible and you want simple type coercion to a JsonArray
. Use toJsonArray
instead when the value
array cannot be guaranteed to be JSON-compatible and you want an assurance of runtime
type safety. This is a shortcut for writing asJsonArray(coerceAnyJson(value))
.
The array to coerce.
-Narrows an array of type T
to a JsonArray
using a shallow type-compatibility check. Use this when the source of
+
Narrows an array of type T
to a JsonArray
using a shallow type-compatibility check. Use this when the source of
the array is known to be JSON-compatible and you want simple type coercion to a JsonArray
. Use toJsonArray
instead when the value
array cannot be guaranteed to be JSON-compatible and you want an assurance of runtime
type safety. This is a shortcut for writing asJsonArray(coerceAnyJson(value)) ?? defaultValue
.
Narrows an object of type T
to a JsonMap
using a shallow type-compatibility check. Use this when the source of
+
Narrows an object of type T
to a JsonMap
using a shallow type-compatibility check. Use this when the source of
the object is known to be JSON-compatible and you want simple type coercion to a JsonMap
. Use toJsonMap
instead when the value
object cannot be guaranteed to be JSON-compatible and you want an assurance of runtime
type safety. This is a shortcut for writing asJsonMap(coerceAnyJson(value))
.
The object to coerce.
-Narrows an object of type T
to a JsonMap
using a shallow type-compatibility check. Use this when the source of
+
Narrows an object of type T
to a JsonMap
using a shallow type-compatibility check. Use this when the source of
the object is known to be JSON-compatible and you want simple type coercion to a JsonMap
. Use toJsonMap
instead when the value
object cannot be guaranteed to be JSON-compatible and you want an assurance of runtime
type safety. This is a shortcut for writing asJsonMap(coerceAnyJson(value)) ?? defaultValue
.
Returns an array of all entry tuples of type [K, NonNullable<T[K]>]
in an object T
whose values are neither
+
Returns an array of all entry tuples of type [K, NonNullable<T[K]>]
in an object T
whose values are neither
null
nor undefined
. This can be convenient for enumerating the entries of unknown objects with optional
properties (including Dictionary
s) without worrying about performing checks against possibly undefined
or
null
values.
See also caveats outlined in entriesOf.
-The object of interest.
-Returns an array of all string
keys in an object of type T
whose values are neither null
nor undefined
.
+
Returns an array of all string
keys in an object of type T
whose values are neither null
nor undefined
.
This can be convenient for enumerating the keys of definitely assigned properties in an object or Dictionary
.
See also caveats outlined in keysOf.
-The object of interest.
-Returns an array of all values of type T
in an object T
for values that are neither null
nor undefined
.
+
Returns an array of all values of type T
in an object T
for values that are neither null
nor undefined
.
This can be convenient for enumerating the values of unknown objects with optional properties (including
Dictionary
s) without worrying about performing checks against possibly undefined
or null
values.
The object of interest.
-Narrows a type Nullable<T>
to a T
or raises an error.
Narrows a type Nullable<T>
to a T
or raises an error.
Use of the type parameter T
to further narrow the type signature of the value being tested is
strongly discouraged unless you are completely confident that the value is of the necessary shape to
conform with T
. This function does nothing at either compile time or runtime to prove the value is of
@@ -6,7 +6,7 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-Optional
message: stringThe error message to use if value
is undefined
or null
.
UnexpectedValueTypeError If the value was undefined.
-UnexpectedValueTypeError If the value was undefined.
+Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns undefined
otherwise.
Narrows an unknown
value to an AnyJson
if it is type-compatible, or returns undefined
otherwise.
See also caveats noted in isAnyJson.
-The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was not a JSON value type.
-Narrows an unknown
value to an Array
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Narrows an unknown
value to an Array
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
+Narrows an unknown
value to a boolean
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
Narrows an unknown
value to a boolean
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Narrows an unknown
value to a Dictionary<T>
if it is type-compatible and tests positively
+
Narrows an unknown
value to a Dictionary<T>
if it is type-compatible and tests positively
with isDictionary, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
+Narrows an unknown
value to an AnyFunction
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
Narrows an unknown
value to an AnyFunction
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Narrows an unknown
value to instance of constructor type T
if it is type-compatible, or raises an error
+
Narrows an unknown
value to instance of constructor type T
if it is type-compatible, or raises an error
otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
+Narrows an AnyJson
value to a JsonArray
if it is type-compatible, or raises an error otherwise.
Narrows an AnyJson
value to a JsonMap
if it is type-compatible, or raises an error otherwise.
Narrows an unknown
value to a number
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
Narrows an unknown
value to a number
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Narrows an unknown
value to an object
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Narrows an unknown
value to an object
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
+Narrows an unknown
value to an object
if it is type-compatible and tests positively with isPlainObject,
+
Narrows an unknown
value to an object
if it is type-compatible and tests positively with isPlainObject,
or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
+Narrows an unknown
value to a string
if it is type-compatible, or raises an error otherwise.
The value to test.
-Optional
message: stringThe error message to use if value
is not type-compatible.
Narrows an unknown
value to a string
if it is type-compatible, or raises an error otherwise.
The value to test.
+Optional
message: stringThe error message to use if value
is not type-compatible.
UnexpectedValueTypeError If the value was undefined.
-Returns the entries of an object of type T
. This is like Object.entries
except the return type
+
Returns the entries of an object of type T
. This is like Object.entries
except the return type
captures the known keys and value types of T
.
Note that it is the responsibility of the caller to use this wisely -- there are cases where the runtime set of entries returned may be broader than the type checked set at compile time, @@ -10,5 +10,5 @@ of simple data objects with known properties.
interface Point { x: number; y: number; }
const point: Point = { x: 1, y: 2 };
// type of entries -> ['x' | 'y', number][]
const entries = entriesOf(point);
for (const entry of entries) {
console.log(entry[0], entry[1]);
}
// x 1
// y 2
-The object of interest.
-Given a deep-search query path, returns an object property or array value of an object or array.
+Given a deep-search query path, returns an object property or array value of an object or array.
const obj = { foo: { bar: ['baz'] } };
const value = get(obj, 'foo.bar[0]');
// type of value -> unknown; value === 'baz'
const value = get(obj, 'foo.bar.nothing', 'default');
// type of value -> unknown; value === 'default'
const value = get(obj, 'foo["bar"][0]');
// type of value -> unknown; value === 'baz'
const arr = [obj];
const value = get(arr, '[0].foo.bar[0]');
// type of value -> unknown; value === 'baz'
-Any value to query.
-The query path.
-Optional
defaultValue: unknownThe default to return if the query result was not defined.
-Any value to query.
+The query path.
+Optional
defaultValue: unknownThe default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of a JsonCollection as an +
Given a deep-search query path, returns an object property or array value of a JsonCollection as an
AnyJson, or undefined
if a value was not found or was not type-compatible.
See coerceAnyJson for caveats regarding shallow type detection of AnyJson
values from untyped sources.
const obj = { foo: { bar: [{ a: 'b' }] } };
const value = getAnyJson(obj, 'foo.bar[0]');
// type of value -> AnyJson; value -> { a: 'b' }
-Given a deep-search query path, returns an object property or array value of a JsonCollection as an +
Given a deep-search query path, returns an object property or array value of a JsonCollection as an AnyJson, or the given default if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ a: 'b' }] } };
const value = getAnyJson(obj, 'foo.bar[1]', { c: 'd' });
// type of value -> AnyJson; value -> { c: 'd' }
-Given a deep-search query path, returns an object property or array value of an object or array as an +
Given a deep-search query path, returns an object property or array value of an object or array as an
AnyArray, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1, 2, 3] } };
const value = getArray(obj, 'foo.bar');
// type of value -> AnyArray; value -> [1, 2, 3]
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an +
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an
AnyArray, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1, 2, 3] } };
const value = getArray(obj, 'foo.baz', [4, 5, 6]);
// type of value -> AnyArray; value -> [4, 5, 6]
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as a boolean
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as a boolean
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [true] } };
const value = getBoolean(obj, 'foo.bar[0]');
// type of value -> boolean; value -> true
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as a boolean
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as a boolean
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [true] } };
const value = getBoolean(obj, 'foo.bar[1]', false);
// type of value -> boolean; value -> false
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as a Dictionary<T>
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as a Dictionary<T>
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getDictionary<string>(obj, 'foo.bar[0]');
// type of value -> Dictionary<string>; value -> { name: 'baz' }
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an Dictionary<T>
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an Dictionary<T>
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getDictionary<string>(obj, 'foo.bar[1]', { name: 'buzz' });
// type of value -> Dictionary<string>; value -> { name: 'buzz' }
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as an +
Given a deep-search query path, returns an object property or array value of an object or array as an
AnyFunction, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [(arg: string) => `Hi, ${arg}`] } };
const value = getFunction(obj, 'foo.bar[0]');
// type of value -> AnyArray; value -> (arg: string) => `Hi, ${arg}`
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an +
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an
AnyFunction, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [(arg: string) => `Hi, ${arg}`] } };
const value = getFunction(obj, 'foo.bar[1]', (arg: string) => `Bye, ${arg}`);
// type of value -> AnyArray; value -> (arg: string) => `Bye, ${arg}`)
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as an instance of +
Given a deep-search query path, returns an object property or array value of an object or array as an instance of
class type C
, or undefined
if a value was not found or was not type-compatible.
class Example { ... }
const obj = { foo: { bar: [new Example()] } };
const value = getInstance(obj, 'foo.bar[0]', Example);
// type of value -> Example
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an instance of +
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an instance of
class type C
, or undefined
if a value was not found or was not type-compatible.
class Example { ... }
const obj = { foo: { bar: [new Example()] } };
const value = getInstance(obj, 'foo.bar[0]', Example);
// type of value -> Example; value -> new Example()
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Given a deep-search query path, returns an object property or array value from an AnyJson as a +
Given a deep-search query path, returns an object property or array value from an AnyJson as a
JsonArray, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1, 2, 3] } };
const value = getJsonArray(obj, 'foo.bar');
// type of value -> JsonArray; value -> [1, 2, 3]
-Given a deep-search query path, returns an object property or array value from an AnyJson as a +
Given a deep-search query path, returns an object property or array value from an AnyJson as a JsonArray, or the given default if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1, 2, 3] } };
const value = getJsonArray(obj, 'foo.baz', [4, 5, 6]);
// type of value -> JsonArray; value -> [4, 5, 6]
-Given a deep-search query path, returns an object property or array value from an AnyJson as a +
Given a deep-search query path, returns an object property or array value from an AnyJson as a
JsonMap, or undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ a: 'b' }] } };
const value = getJsonMap(obj, 'foo.bar[0]');
// type of value -> JsonMap; value -> { a: 'b' }
-Given a deep-search query path, returns an object property or array value from an AnyJson as a +
Given a deep-search query path, returns an object property or array value from an AnyJson as a JsonMap, or the given default if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ a: 'b' }] } };
const value = getJsonMap(obj, 'foo.bar[1]', { c: 'd' });
// type of value -> JsonMap; value -> { c: 'd' }
-Given a deep-search query path, returns an object property or array value of an object or array as a number
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as a number
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1] } };
const value = getNumber(obj, 'foo.bar[0]');
// type of value -> number; value -> 1
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as a number
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as a number
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [1] } };
const value = getNumber(obj, 'foo.bar[1]', 2);
// type of value -> number; value -> 2
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getObject(obj, 'foo.bar[0]');
// type of value -> object; value -> { name: 'baz' }
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getObject(obj, 'foo.bar[1]', { name: 'buzz' });
// type of value -> object; value -> { name: 'buzz' }
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
undefined
if a value was not found or was not type-compatible. This differs from getObject by way of
testing for the property value type compatibility using isPlainObject instead of isObject.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getPlainObject(obj, 'foo.bar[0]');
// type of value -> object; value -> { name: 'baz' }
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as an object
, or
undefined
if a value was not found or was not type-compatible. This differs from getObject by way of
testing for the property value type compatibility using isPlainObject instead of isObject.
const obj = { foo: { bar: [{ name: 'baz' }] } };
const value = getPlainObject(obj, 'foo.bar[1]', { name: 'buzz' });
// type of value -> object; value -> { name: 'buzz' }
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Given a deep-search query path, returns an object property or array value of an object or array as a string
, or
+
Given a deep-search query path, returns an object property or array value of an object or array as a string
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: ['baz'] } };
const value = getString(obj, 'foo.bar[0]');
// type of value -> string; value -> 'baz'
-Any value to query.
-The query path.
-Given a deep-search query path, returns an object property or array value of an object or array as a string
, or
+
Any value to query.
+The query path.
+Given a deep-search query path, returns an object property or array value of an object or array as a string
, or
undefined
if a value was not found or was not type-compatible.
const obj = { foo: { bar: ['baz'] } };
const value = getString(obj, 'foo.bar[1]', 'default');
// type of value -> string; value -> 'default'
-Any value to query.
-The query path.
-The default to return if the query result was not defined.
-Any value to query.
+The query path.
+The default to return if the query result was not defined.
+Tests whether a value of type T
contains one or more property keys
. If so, the type of the tested value is
+
Tests whether a value of type T
contains one or more property keys
. If so, the type of the tested value is
narrowed to reflect the existence of those keys for convenient access in the same scope. Returns false if the
property key does not exist on the target type, which must be an object. Returns true if the property key exists,
even if the associated value is undefined
or null
.
// type of obj -> unknown
if (has(obj, 'name')) {
// type of obj -> { name: unknown }
if (has(obj, 'data')) {
// type of obj -> { name: unknown } & { data: unknown }
} else if (has(obj, ['error', 'status'])) {
// type of obj -> { name: unknown } & { error: unknown, status: unknown }
}
}
-The value to test.
-One or more string
keys to check for existence.
Tests whether a value of type Tests whether a value of type The value to test.T
contains a property key
of type AnyJson, using a shallow test for
+Function hasAnyJson
T
contains a property key
of type AnyJson, using a shallow test for
AnyJson
compatibility (see isAnyJson for more information). If so, the type of the
tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns
false
if the property key does not exist on the object or the value stored by that key is not of type
AnyJson.
-// type of obj -> unknown
if (hasAnyJson(obj, 'body')) {
// type of obj -> { body: AnyJson }
}
Type Parameters
T
K extends string
Parameters
value: Optional<T>
key: K
Returns value is T & object & View<K, AnyJson>
Tests whether a value of type T
contains a property key
of type AnyArray. If so, the type of the tested
+
Tests whether a value of type T
contains a property key
of type AnyArray. If so, the type of the tested
value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if
the property key does not exist on the object or the value stored by that key is not of type AnyArray.
// type of obj -> unknown
if (hasNumber(obj, 'offset')) {
// type of obj -> { offset: number }
if (hasNumber(obj, 'page') && hasArray(obj, 'items')) {
// type of obj -> { offset: number } & { page: number } & { items: AnyArray }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
of type boolean
. If so, the type of the tested value is
+
Tests whether a value of type T
contains a property key
of type boolean
. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if the
property key does not exist on the object or the value stored by that key is not of type boolean
.
// type of obj -> unknown
if (hasBoolean(obj, 'enabled')) {
// type of obj -> { enabled: boolean }
if (hasBoolean(obj, 'hidden')) {
// type of obj -> { enabled: boolean } & { hidden: boolean }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
whose type tests positively when tested with
+
Tests whether a value of type T
contains a property key
whose type tests positively when tested with
isDictionary. If so, the type of the tested value is narrowed to reflect the existence of that key for
convenient access in the same scope. Returns false
if the property key does not exist on the object or the value
stored by that key is not of type object
.
// type of obj -> unknown
if (hasNumber(obj, 'status')) {
// type of obj -> { status: number }
if (hasDictionary(obj, 'data')) {
// type of obj -> { status: number } & { data: Dictionary }
} else if (hasString('error')) {
// type of obj -> { status: number } & { error: string }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
of type AnyFunction. If so, the type of the
+
Tests whether a value of type T
contains a property key
of type AnyFunction. If so, the type of the
tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns
false
if the property key does not exist on the object or the value stored by that key is not of type
AnyFunction.
// type of obj -> unknown
if (hasFunction(obj, 'callback')) {
// type of obj -> { callback: AnyFunction }
obj.callback(response);
}
-The value to test.
-Tests whether a value of type T
contains a property key
whose type tests positively when tested with
+
Tests whether a value of type T
contains a property key
whose type tests positively when tested with
isInstance when compared with the given constructor type C
. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if the
property key does not exist on the object or the value stored by that key is not an instance of C
.
class ServerResponse { ... }
// type of obj -> unknown
if (hasNumber(obj, 'status')) {
// type of obj -> { status: number }
if (hasInstance(obj, 'data', ServerResponse)) {
// type of obj -> { status: number } & { data: ServerResponse }
} else if (hasString('error')) {
// type of obj -> { status: number } & { error: string }
}
}
-The value to test.
-Tests whether a value of type T extends AnyJson
contains a property key
of type JsonArray. If so, the
+
Tests whether a value of type T extends AnyJson
contains a property key
of type JsonArray. If so, the
type of the tested value is narrowed to reflect the existence of that key for convenient access in the same scope.
Returns false
if the property key does not exist on the object or the value stored by that key is not of type
JsonArray.
// type of obj -> unknown
if (hasJsonArray(obj, 'body')) {
// type of obj -> { body: JsonArray }
}
-The value to test.
-Tests whether a value of type T extends AnyJson
contains a property key
of type JsonMap. If so, the type
+
Tests whether a value of type T extends AnyJson
contains a property key
of type JsonMap. If so, the type
of the tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returns
false
if the property key does not exist on the object or the value stored by that key is not of type
JsonMap.
// type of obj -> unknown
if (hasJsonMap(obj, 'body')) {
// type of obj -> { body: JsonMap }
}
-The value to test.
-Tests whether a value of type T
contains a property key
of type number
. If so, the type of the tested value is
+
Tests whether a value of type T
contains a property key
of type number
. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if the
property key does not exist on the object or the value stored by that key is not of type number
.
// type of obj -> unknown
if (hasNumber(obj, 'offset')) {
// type of obj -> { offset: number }
if (hasNumber(obj, 'page') && hasArray(obj, 'items')) {
// type of obj -> { offset: number } & { page: number } & { items: unknown[] }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
of type object
. If so, the type of the tested value is
+
Tests whether a value of type T
contains a property key
of type object
. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if the
property key does not exist on the object or the value stored by that key is not of type object
.
// type of obj -> unknown
if (hasNumber(obj, 'status')) {
// type of obj -> { status: number }
if (hasObject(obj, 'data')) {
// type of obj -> { status: number } & { data: object }
} else if (hasString('error')) {
// type of obj -> { status: number } & { error: string }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
whose type tests positively when tested with
+
Tests whether a value of type T
contains a property key
whose type tests positively when tested with
isPlainObject. If so, the type of the tested value is narrowed to reflect the existence of that key for
convenient access in the same scope. Returns false
if the property key does not exist on the object or the value
stored by that key is not of type object
.
// type of obj -> unknown
if (hasNumber(obj, 'status')) {
// type of obj -> { status: number }
if (hasPlainObject(obj, 'data')) {
// type of obj -> { status: number } & { data: object }
} else if (hasString('error')) {
// type of obj -> { status: number } & { error: string }
}
}
-The value to test.
-Tests whether a value of type T
contains a property key
of type string
. If so, the type of the tested value is
+
Tests whether a value of type T
contains a property key
of type string
. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false
if the
property key does not exist on the object or the value stored by that key is not of type string
.
// type of obj -> unknown
if (hasString(obj, 'name')) {
// type of obj -> { name: string }
if (hasString(obj, 'message')) {
// type of obj -> { name: string } & { message: string }
}
}
-The value to test.
-Tests whether an unknown
value is an Array
.
Tests whether an unknown
value is an Array
.
Use of the type parameter T
to further narrow the type signature of the value being tested is
strongly discouraged unless you are completely confident that the value is of the necessary shape to
conform with T
. This function does nothing at either compile time or runtime to prove the value is of
@@ -6,5 +6,5 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-The value to test.
+Tests whether an unknown
value conforms to AnyArrayLike.
Tests whether an unknown
value conforms to AnyArrayLike.
Use of the type parameter T
to further narrow the type signature of the value being tested is
strongly discouraged unless you are completely confident that the value is of the necessary shape to
conform with T
. This function does nothing at either compile time or runtime to prove the value is of
@@ -6,5 +6,5 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-The value to test.
+Tests whether an unknown
value is a class constructor that is either equal to or extends another class
+
Tests whether an unknown
value is a class constructor that is either equal to or extends another class
constructor.
The value to test.
-The class to test against.
-The value to test.
+The class to test against.
+A shortcut for testing the suitability of a value to be used as a Dictionary<T>
type. Shorthand for
+
A shortcut for testing the suitability of a value to be used as a Dictionary<T>
type. Shorthand for
writing isPlainObject<Dictionary<T>>(value)
. While some non-plain-object types are compatible with
index signatures, they were less typically used as such, so this function focuses on the 80% case.
Use of the type parameter T
to further narrow the type signature of the value being tested is
@@ -8,5 +8,5 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-The value to test.
+Tests whether an unknown
value is a function
.
The value to test.
-Tests whether an unknown
value is a function
.
The value to test.
+Tests whether an unknown
value is a function
.
The value to test.
-Tests whether an unknown
value is a function
.
The value to test.
+Tests whether an unknown
value is an Object
subtype (e.g., arrays, functions, objects, regexes,
+
Tests whether an unknown
value is an Object
subtype (e.g., arrays, functions, objects, regexes,
new Number(0), new String(''), and new Boolean(true)). Tests that wish to distinguish objects that
were created from literals or that otherwise were not created via a non-Object
constructor and do
not have a prototype chain should instead use isPlainObject.
T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
-The value to test.
-The value to test.
+Tests whether or not an unknown
value is a plain JavaScript object. That is, if it is an object created
+
Tests whether or not an unknown
value is a plain JavaScript object. That is, if it is an object created
by the Object constructor or one with a null prototype
.
Use of the type parameter T
to further narrow the type signature of the value being tested is
strongly discouraged unless you are completely confident that the value is of the necessary shape to
@@ -7,5 +7,5 @@
bad practice unless you have performed some other due diligence in proving that the value must be of
shape T
. Use of the functions in the has
co-library are useful for performing such full or partial
proofs.
The value to test.
-The value to test.
+Returns the keys of an object of type T
. This is like Object.keys
except the return type
+
Returns the keys of an object of type T
. This is like Object.keys
except the return type
captures the known keys of T
.
Note that it is the responsibility of the caller to use this wisely -- there are cases where the runtime set of keys returned may be broader than the type checked set at compile time, @@ -10,5 +10,5 @@ of simple data objects with known properties.
interface Point { x: number; y: number; }
const point: Point = { x: 1, y: 2 };
const keys = keysOf(point);
// type of keys -> ('a' | 'b')[]
for (const key of keys) {
console.log(key, point[key]);
}
// x 1
// y 2
-The object of interest.
-Narrows an object of type T
to an AnyJson
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to an AnyJson
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceAnyJson(unknown)
to type-narrow an arbitrary value to an AnyJson
when the value's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Use coerceAnyJson(unknown)
when the value
object can be guaranteed to be JSON-compatible
and only needs type coercion.
The value to convert.
+JsonCloneError If the value values contain circular references.
-Narrows an object of type T
to an AnyJson
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to an AnyJson
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceAnyJson(unknown)
to type-narrow an arbitrary value to an AnyJson
when the value's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Use coerceAnyJson(unknown)
when the value
object can be guaranteed to be JSON-compatible
and only needs type coercion.
Narrows an array of type T
to a JsonArray
following a deep, brute-force conversion of the array's data to
+
Narrows an array of type T
to a JsonArray
following a deep, brute-force conversion of the array's data to
only consist of JSON-compatible values by performing a basic JSON clone on the array. This is preferable to
using the weaker coerceJsonArray(array)
to type-narrow an arbitrary array to a JsonArray
when the array's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Non-JSON entries will be converted to null
s. Use coerceJsonArray(array)
when the value
object can be guaranteed to be JSON-compatible and only needs type coercion.
The array to convert.
+The array to convert.
JsonCloneError If the array values contain circular references.
-Narrows an array of type T
to a JsonArray
following a deep, brute-force conversion of the array's data to
+
Narrows an array of type T
to a JsonArray
following a deep, brute-force conversion of the array's data to
only consist of JSON-compatible values by performing a basic JSON clone on the array. This is preferable to
using the weaker coerceJsonArray(array)
to type-narrow an arbitrary array to a JsonArray
when the array's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Non-JSON entries will be converted to null
s. Use coerceJsonArray(array)
when the value
object can be guaranteed to be JSON-compatible and only needs type coercion.
The array to convert.
+JsonCloneError If the array values contain circular references.
-Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceJsonMap(object)
to type-narrow an arbitrary array to a JsonMap
when the object's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Non-JSON entries will be converted to null
s. Use coerceJsonArray(array)
when the value
object can be guaranteed to be JSON-compatible and only needs type coercion.
Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceJsonMap(object)
to type-narrow an arbitrary object to a JsonMap
when the object's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Use coerceJsonMap(object)
when the value
object can be guaranteed to be JSON-compatible
and only needs type coercion.
The object to convert.
+The object to convert.
JsonCloneError If the object values contain circular references.
-Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceJsonMap(object)
to type-narrow an arbitrary object to a JsonMap
when the object's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Use coerceJsonMap(object)
when the value
object can be guaranteed to be JSON-compatible
and only needs type coercion.
The object to convert.
+JsonCloneError If the object values contain circular references.
-Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
+
Narrows an object of type T
to a JsonMap
following a deep, brute-force conversion of the object's data to
only consist of JSON-compatible values by performing a basic JSON clone on the object. This is preferable to
using the weaker coerceJsonMap(object)
to type-narrow an arbitrary object to a JsonMap
when the object's source
is unknown, but it comes with the increased overhead of performing the deep JSON clone to ensure runtime type
safety. The use of JSON cloning guarantees type safety by omitting non-JSON-compatible elements from the resulting
JSON data structure. Use coerceJsonMap(object)
when the value
object can be guaranteed to be JSON-compatible
and only needs type coercion.
Returns the values of an object of type T
. This is like Object.values
except the return type
+
Returns the values of an object of type T
. This is like Object.values
except the return type
captures the possible value types of T
.
Note that it is the responsibility of the caller to use this wisely -- there are cases where the runtime set of values returned may be broader than the type checked set at compile time, @@ -10,5 +10,5 @@ of simple data objects with known properties.
interface Point { x: number; y: number; }
const point: Point = { x: 1, y: 2 };
const values = valuesOf(point);
// type of values -> number[]
for (const value of values) {
console.log(value);
}
// 1
// 2
-The object of interest.
-This is a simple TypeScript-oriented library developed for use in Salesforce TypeScript libraries, applications, and plugins consisting of two parts:
+This is a simple TypeScript-oriented library developed for use in Salesforce TypeScript libraries, applications, and plugins consisting of two parts:
const pets: Dictionary<string> = {
fido: 'dog',
bill: 'cat',
fred: undefined
};
// note that the array is typed as [string, string] rather than [string, string | undefined]
function logPet([name, type]: [string, string]) {
console.log('%s is a %s', name, type);
}
definiteEntriesOf(pets).forEach(logPet);
// fido is a dog
// bill is a cat
Another Salesforce TypeScript library, @salesforce/kit, builds on this library to add additional utilities. It includes additional JSON support, a lightweight replacement for some lodash
functions, and growing support for patterns used in other Salesforce CLI libraries and applications.
An alias for an array of T
elements, where T
defaults to unknown
.
Any object with both a numeric index signature with values of type T
and a numeric length
+
Any object with both a numeric index signature with values of type T
and a numeric length
property. T
defaults to unknown
if unspecified.
An object with arbitrary string-indexed values of an optional generic type Optional<T>
. T
defaults to unknown
+
An object with arbitrary string-indexed values of an optional generic type Optional<T>
. T
defaults to unknown
when not explicitly supplied. For convenient iteration of definitely assigned (i.e. non-nullable) entries, keys,
and values, see the following functions: definiteEntriesOf, definiteKeysOf, and
definiteValuesOf.
Any JSON-compatible array.
+Any JSON-compatible object.
+A constructor for any type T
. T
defaults to object
when not explicitly supplied.
Rest
...args: any[]Any function
returning type T
. T
defaults to unknown
when not explicitly supplied.
Rest
...args: any[]Any valid JSON primitive value.
-Any valid JSON primitive value.
+An alias for the commonly needed Extract<keyof T, string>
.
An alias for a tuple of type [string, T]' for a given generic type
T.
Tdefaults to
unknown` if not otherwise
+
Extracts literally defined property names from a type T
as a union of key name strings, minus
+
Extracts literally defined property names from a type T
as a union of key name strings, minus
any index signatures.
Creates a new Record
type from the literal properties of a type T
, assigning their values
+
Creates a new Record
type from the literal properties of a type T
, assigning their values
the to the type U
.
This can be useful for creating interfaces from the keys of an enum
so that the keys are
available at runtime for meta-programming purposes, while both tying the properties of the
generated type to the enum keys and remaining as DRY as possible.
enum QUERY_KEY { id, name, created, updated }
// type of QUERY_KEY -> {
// [x: number]: number;
// readonly id: number;
// readonly name: number;
// readonly created: number;
// readonly updated: number;
// }
interface QueryRecord extends LiteralsRecord<typeof QUERY_KEY, string> { }
// type of QueryRecord -> {
// readonly id: string;
// readonly name: string;
// readonly created: string;
// readonly updated: string;
// }
// And for an interface with writable properties, use the following:
interface QueryRecord extends ReadWrite<LiteralsRecord<typeof QUERY_KEY, string>> { }
// type of QueryRecord -> {
// id: string;
// name: string;
// created: string;
// updated: string;
// }
-A union type for either the parameterized type T
or an array of T
.
Subtracts undefined
from any union type T
. This is the opposite of Optional.
A union type for either the parameterized type T
, null
, or undefined
-- the opposite of
+
Creates a new type that omits keys in union type K
of a target type T
.
A union type for either the parameterized type T
or undefined
-- the opposite of NonOptional.
A union type for either the parameterized type T
or undefined
-- the opposite of NonOptional.
Returns a new type consisting of all properties declared for an input type T2
overlaid on the
+
Returns a new type consisting of all properties declared for an input type T2
overlaid on the
properties of type T1
. Any definitions in T2
replace those previously defined in T1
. This can
be useful for redefining the types of properties on T1
with values from an inline type T2
, perhaps to
change their type or to make them optional.
type NameAndStringValue = { name: string, value: string }
type NameAndOptionalNumericValue = Overwrite<NameAndValue, { value?: number }>
// type of NameAndOptionalNumericValue -> { name: string } & { value?: number | undefined }
-Converts readonly properties of a type T
to writable properties. This is the opposite of the
+
Converts a type T
that may have optional, nullable properties into a new type with only required
+
Converts a type T
that may have optional, nullable properties into a new type with only required
properties, while also subtracting null
from all possible property values.
type Foo = { bar?: string | undefined | null };
type RequiredNonNullableFoo = RequiredNonNullable<Foo>;
// RequiredNonNullableFoo -> { bar: string };
-Converts a type T
that may have optional properties into a type T
with only required
+
Converts a type T
that may have optional properties into a type T
with only required
properties (e.g. undefined
values are not allowed). Explicit null
s in value unions
will still be possible. This is similar to the Required
builtin mapped type, but also
subtracts undefined
from value union types as well as the optional property declaration.
type Foo = { bar?: string | undefined | null };
type RequiredNonOptionalFoo = RequiredNonOptional<Foo>;
// RequiredNonOptionalFoo -> { bar: string | null };
-A view over an object
with constrainable properties.
Narrows an
-unknown
value to anArray
if it is type-compatible, or returnsundefined
otherwise.