Gathering detailed insights and metrics for @teamteanpm2024/cum-maiores-eum
Gathering detailed insights and metrics for @teamteanpm2024/cum-maiores-eum
Gathering detailed insights and metrics for @teamteanpm2024/cum-maiores-eum
Gathering detailed insights and metrics for @teamteanpm2024/cum-maiores-eum
npm install @teamteanpm2024/cum-maiores-eum
Typescript
Module System
Node Version
NPM Version
54.9
Supply Chain
94.7
Quality
79.2
Maintenance
100
Vulnerability
100
License
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Latest Version
1.1.3
Package Id
@teamteanpm2024/cum-maiores-eum@1.1.3
Unpacked Size
14.01 kB
Size
5.45 kB
File Count
6
NPM Version
10.5.0
Node Version
20.12.2
Publised On
29 Apr 2024
Cumulative downloads
Total Downloads
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
41
A simple TypeScript library providing type guards for the primitive types in JavaScript.
TypeScript will use certain clues to narrow the typing information down as much as it can before runtime. To help the compiler do this, TypeScript provides two features that this library uses to provide convenience functions:
To install, just run:
1npm install @teamteanpm2024/cum-maiores-eum
assertIsObject
isObject
assertIsArray
isArray
assertIsArrayOf
isArrayOf
assertIsString
isString
assertIsNumber
isNumber
assertIsBoolean
isBoolean
assertIsNull
isNull
assertIsUndefined
isUndefined
assertIsBigInt
isBigInt
assertIsSymbol
isSymbol
assertHasProperty
hasProperty
Everything is available from the top level. Here's an example of how to import the functions:
1import { assertIsString } from "@teamteanpm2024/cum-maiores-eum"; 2 3const [firstEntry, , , fourthEntry] = someCommaSeparatedString.split(","); 4// Both could either be a string, or undefined 5assertIsString(firstEntry); 6assertIsString(fourthEntry); 7// Both are now recognized as strings (or would've thrown an error)
Once an object is found to be of type object, TypeScript won't just allow access to properties it doesn't know are there or not. Luckily, this library also provides convenient functions for checking this, as well:
1const obj: object = {}; 2 3assertHasProperty(obj, "thing"); 4// Now recognizes obj as type: object & Record<"thing", unknown> 5assertIsString(obj.thing); 6// Now recognizes obj as having a 'thing' property of type 'string' 7console.log(obj.thing.toUppercase());
Each type assertion function throw their own type of error. Each of those errors, however, extends AssertionError
(defined and provided by this library), which itself extends TypeError
. This should make it convenient for catching
thrown errors as desired while also allowing them to be easily distinguished from other types of errors.
To import the error classes to compare against during a try
/catch
, simply import them from the package like so:
1import { StringAssertionError } from "@teamteanpm2024/cum-maiores-eum";
These are the available error types, grouped according to their inheritance:
AssertionError
ObjectAssertionError
ArrayAssertionError
ArrayMemberAssertionError
StringAssertionError
NumberAssertionError
BooleanAssertionError
NullAssertionError
UndefinedAssertionError
BigIntAssertionError
SymbolAssertionError
PropertyAssertionError
Type predicate functions take in
an argument, and return a boolean that is true
if the passed argument was the expected type, or false
if it isn't.
For example:
1import { isString } from "@teamteanpm2024/cum-maiores-eum"; 2 3function doSomething(myArg: string): number; 4function doSomething(myArg: number): string; 5function doSomething(myArg: any): number | string { 6 if (isString(myArg)) { 7 return 42; 8 } 9 return "you gave me a number"; 10} 11 12const aNumber = doSomething("definitely a string"); // The compiler will know 'aNumber' is a number. 13const aString = doSomething(3); // The compiler will know 'aString' is a string.
Type assertion functions work much the same way as predicates, except they throw an error if the argument passed isn't of the expected type. This is particularly useful when you're pretty sure something is a given type but you don't wanna have to mess around with flow control. Simply plop an assertion down and everything after it assumes the value is that type.
For example:
1import { assertIsString } from "@teamteanpm2024/cum-maiores-eum"; 2 3function printUppercase(myArg: any) { 4 assertIsString(myArg); 5 console.log(myArg.toUppercase()); // compiler doesn't complain 6}
isArrayOf
and assertIsArrayOf
These are more advanced than the other predicates and assertions. These take a second argument to check against each
member of the array. isArrayOf
and assertIsArrayOf
both accept a type predicate for this argument, but
assertIsArrayOf
can also accept a type assertion. If a type assertion is provided, assertIsArrayOf
will make no
attempt to stop any errors that assertion might throw. However, if the object passed is an array, and a type predicate
was passed, but its members fail the type predicate check, it will throw an ArrayMemberAssertionError
which extends
the normal ArrayAssertionError
.
Here's some examples of how these are used:
1let stringArray: unknown = ["hello", "world"]; 2if (isArrayOf(stringArray, isString)) { 3 // We're guaranteed that stringArray is an array of strings here 4}
1try { 2 const obj: unknown = [123] 3 assertIsArrayOf<string>(obj, isString) 4} catch (err) { 5 if (err instanceof ArrayAssertionError) { 6 // the object is not an array at all. 7 // NOTE: In this example, this WILL NOT be the error thrown. 8 } else if (err instanceof ArrayMemberAssertionError) { 9 // the object is an array, but one or more of its members are not strings 10 // NOTE: In this example, this WILL be the error that will be thrown. 11 } else if (err instanceof StringAssertionError) { 12 // the object is an array, but one or more of its members are not strings 13 // NOTE: In this example, this WILL NOT be the error thrown. 14 } 15}
1try { 2 const obj: unknown = [123] 3 assertIsArrayOf<string>(obj, assertIsString) 4} catch (err) { 5 if (err instanceof ArrayAssertionError) { 6 // the object is not an array at all. 7 // NOTE: In this example, this WILL NOT be the error thrown. 8 } else if (err instanceof ArrayMemberAssertionError) { 9 // the object is an array, but one or more of its members are not strings 10 // NOTE: In this example, this WILL NOT be the error thrown. 11 } else if (err instanceof StringAssertionError) { 12 // the object is an array, but one or more of its members are not strings 13 // NOTE: In this example, this is the error that WILL be thrown. 14 } 15}
object
and null
The typeof
operator, as its name suggests, returns the name of the type of the value provided as a string. For
example, typeof "hello"
would return "string"
. But there's a long existing "bug" in JavaScript
that results in typeof null
returning "object"
. This manifests in TypeScript in many ways that can prove to be
frustrating.
TypeScript tries to help out by preventing us from passing null
where things are explicitely typed as object
, but
things can get messy and confusing. In order to properly check that something is actually an object
(and not null
)
we have to do the following:
1const someVar: any = {}; 2if (someVar !== null && typeof someVar === "object") { 3 // 'someVar' is still recognized as type 'any' 4 someVar.thing(); // Compiler doesn't complain 5}
The problem is that even after that check, the compiler will still see it as type any
. This can be a real problem when
trying to be very strict with the typing, since the compiler won't be making sure you can only reference properties it
knows are there.
Luckily, this package provides that with convenience functions:
1const someVar: any = {}; 2if (isObject(someVar)) { 3 // 'someVar' is now recognized as type 'object' 4 someVar.thing(); // Compiler complains 5}
Note: Checking the type directly can work to determine whether or not the value is actually an object
if the
original type is unknown
, because unknown
is inherently more restrictive than any
. The functions provided by this
library still simplify the logic a bit and reduce the need for checking certain edge cases ("We test it so you don't
have to!").
No vulnerabilities found.
No security vulnerabilities found.