Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typedescriptor

Package Overview
Dependencies
Maintainers
5
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typedescriptor - npm Package Compare versions

Comparing version 3.0.2 to 4.0.0-internal.1

.idea/codeStyles/codeStyleConfig.xml

10

package.json
{
"name": "typedescriptor",
"version": "3.0.2",
"version": "4.0.0-internal.1",
"description": "typedescriptor identifies and describes types.",

@@ -15,8 +15,8 @@ "contributors": [

],
"main": "build/lib/TypeDescriptor.js",
"types": "build/lib/TypeDescriptor.d.ts",
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
"dependencies": {},
"devDependencies": {
"assertthat": "5.2.6",
"roboter": "11.6.30",
"assertthat": "6.0.0",
"roboter": "11.6.40",
"semantic-release-configuration": "2.0.0"

@@ -23,0 +23,0 @@ },

@@ -23,6 +23,22 @@ # typedescriptor

First you need to add a reference to typedescriptor to your application:
First you need to import the functions you are interested in:
```javascript
const { Type } = require('typedescriptor');
const {
isArray,
isBoolean,
isError,
isFunction,
isMap,
isNull,
isNumber,
isObject,
isReference,
isScalar,
isSet,
isString,
isSymbol,
isUndefined,
typeOf
} = require('typedescriptor');
```

@@ -33,62 +49,61 @@

```typescript
import { Type } from 'typedescriptor';
import {
isArray,
isBoolean,
isError,
isFunction,
isMap,
isNull,
isNumber,
isObject,
isReference,
isScalar,
isSet,
isString,
isSymbol,
isUndefined,
typeOf
} from 'typedescriptor';
```
Then, to identify a value's type, call the static `of` function and hand over the value:
Then, use the type-guards to determine a variable's type or to narrow a variable's type in TypeScript:
```javascript
const type = Type.of('the native web');
console.log(type);
// => 'string'
```typescript
if (isNumber(value)) {
// Do something with the number.
}
```
The types `array`, `boolean`, `function`, `null`, `number`, `object`, `string`, `symbol` and `undefined` are supported.
The types `array`, `boolean`, `error`, `function`, `map`, `null`, `number`, `object`, `set`, `string`, `symbol` and `undefined` are supported.
### Getting detailed type descriptions
### Getting a variable's type name
To get a detailed type descriptor object, call the static `from` function and hand over the value:
To get a variable's type as a string, use `typeOf`:
```javascript
const typeDescriptor = Type.from('the native web');
console.log(typeDescriptor);
// => {
// name: 'string',
//
// isValueType: true,
// isReferenceType: false,
//
// isArray: false,
// isBoolean: false,
// isFunction: false,
// isNull: false,
// isNumber: false,
// isObject: false,
// isString: true,
// isSymbol: false,
// isUndefined: false
// }
```typescript
const typeName = typeOf('foo');
//=> 'string'
```
### Using TypeScript type guards
This is *not* compatible with the builtin `typeof` operator. Most notably: `null` is not considered to be an `object`, since TypeScript differentiates between the two. `null` is considered a separate type.
Since using the descriptors above doesn't tell TypeScript anything, there are type guards for every type check:
## Caveats
The `isObject` predicate overlaps with multiple others. If, for example, you want to treat a variable differently based on whether it is an array or any other object, you have to first check whether the variable is an array and only then check whether it is an object. If you would first check whether the variable is an object, the result would be `true` even for arrays.
```typescript
import Type from 'typedescriptor';
const someValue = getSomeValue() as any;
if (Type.isArray(someValue)) {
// TypeScript now realizes that `someValue` is of type []
const doStuff = function (value: any): void {
if (isArray(value)) {
// Do things with the array and return!
}
if (isObject(value)) {
// Do things with the object, which now can not be an array.
}
}
if (Type.isValueType(someValue)) {
// TypeScript now realizes that `someValue` is of type string | number | boolean | null | undefined
}
```
## Running quality assurance
The same is true for `isError`, `isMap` and `isSet`.
## Running the quality assurance
To run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):

@@ -95,0 +110,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc