Gathering detailed insights and metrics for core-types
Gathering detailed insights and metrics for core-types
Gathering detailed insights and metrics for core-types
Gathering detailed insights and metrics for core-types
Generic type declarations for e.g. TypeScript and JSON Schema
npm install core-types
Typescript
Module System
Min. Node Version
Node Version
NPM Version
95.1
Supply Chain
100
Quality
76.1
Maintenance
100
Vulnerability
100
License
TypeScript (99.81%)
JavaScript (0.19%)
Total Downloads
2,408,130
Last Day
491
Last Week
22,127
Last Month
114,733
Last Year
1,031,246
MIT License
16 Stars
41 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Jan 29, 2024
Minified
Minified + Gzipped
Latest Version
3.1.0
Package Id
core-types@3.1.0
Unpacked Size
61.77 kB
Size
16.17 kB
File Count
27
NPM Version
9.5.0
Node Version
18.15.0
Published on
Apr 02, 2023
Cumulative downloads
Total Downloads
Last Day
58.9%
491
Compared to previous day
Last Week
-55.8%
22,127
Compared to previous week
Last Month
35.8%
114,733
Compared to previous month
Last Year
48.6%
1,031,246
Compared to previous year
This package provides TypeScript types describing core types useful in TypeScript, JavaScript, JSON, JSON Schema etc. It also contains functions for simplifying unnecessarily complex types, as well helper utilities for other packages converting to/from core-types and another type system.
Using core-types, e.g. implementing conversions to other type systems is easy, since core-types is relatively small and well defined.
true
, false
){ or: [ core-types... ] }
{ and: [ core-types... ] }
The above describes JSON completely, and is a lowest common denominator for describing types useful for JSON, JSON Schema and TypeScript. Think of it as an extremely simplified version of JSON Schema.
This package is used by:
core-types-json-schema
core-types-ts
core-types-graphql
core-types-suretype
typeconv
Since 3.0 this package is pure ESM and requires Node 14.13.1 or later.
To create a core-types type, just cast it to NodeType
.
1import type { NodeType } from 'core-types' 2 3const myStringType: NodeType = { type: 'string' };
For more information on the specific types, see the Specification.
The function simplify
can take a type, or an array of types, and returns simplified type definitions.
Examples of simpifications performed:
and
or or
will often be removed.any
and string
), except for usages const
and enum
, can be simplified as just any
.any
and string
can be simplified to string
.or
containing child or
s, will be flattened to the parent or
.The simplify function is type-wise lossless, but can remove annotations (e.g. descriptions). It is however usually recommended to perform a simplification after a type has been converted to core-types before converting to another type system.
1import { simplify } from 'core-types' 2 3const simplified = simplify( myType ); 4 5simplify( { 6 type: 'or', 7 or: [ 8 { type: 'or', or: [ { type: 'string' } ] }, 9 { type: 'any', const: 'foo' } 10 ] 11} ); // { type: 'string', const: 'foo' }
The validate
function validates that a NodeType
type tree is valid.
It ensures e.g.
minItems
1import { validate } from 'core-types' 2 3validate( myType ); // Throws error if not valid
The traverse
function traverses a type tree and calls a callback function for every node it finds.
The callback function gets an object as argument on the following form:
1interface TraverseCallbackArgument 2{ 3 node: NodeType; 4 rootNode: NodeType; 5 path: Array< string | number >; 6 parentProperty?: string; 7 parentNode?: NodeType; 8 index?: string | number; 9 required?: boolean; 10}
1import { traverse } from 'core-types' 2 3traverse( rootNode, ( { node } ) => { 4 if ( !node.title ) 5 node.title = "This is a dummy title"; 6} );
The some
function is similar to traverse
but the callback can return a boolean. If the callback returns true, some
returns true, otherwise false.
This is useful to quickly find if a node satisifes a certain criteria, and is similar to Array.prototype.some
.
1import { some } from 'core-types' 2 3const hasRefNode = some( rootNode, ( { node: { type } } ) => type === 'ref' );
When implementing conversions to and from core-types, the following helper functions may come in handy:
ensureArray
converts values to arrays of such values, or returns arrays as-is. null and undefined become empty arrayisPrimitiveType
returns true for primitive NodeType
shasConstEnum
returns true for NodeType
s which has (or can have) const
and enum
properties.isEqual
deep-equal comparison (of JSON compatible non-recursive types)intersection
returns an array of values found in both of two arrays. Handles primitives as well as arrays and objects (uses isEqual
)union
returns an array of unique values from two arrays. Handles primitives as well as arrays and objects (uses isEqual
)isNonNullable
isCoreTypesError
decorateErrorMeta
decorateError
getPositionOffset
mergeLocations
mergeAnnotations
extractAnnotations
stringifyAnnotations
stripAnnotations
stringify
When converting, a conversion package is recommended to return a ConversionResult<T>
, i.e. the data as property data
in an object which also contains information about the conversion:
1interface ConversionResult< T = string > 2{ 3 data: T; 4 convertedTypes: Array< string >; 5 notConvertedTypes: Array< string >; 6}
The main type is called NodeType
and is a union of the specific types. A NodeType
always has a type
property of the type Types
. The Types
is defined as:
1type Types = 2 | 'any' 3 | 'null' 4 | 'boolean' 5 | 'string' 6 | 'number' 7 | 'integer' 8 | 'object' 9 | 'array' 10 | 'tuple' 11 | 'ref' 12 | 'and' 13 | 'or';
Depending on which type is used, other properties in NodeType
will be required. In fact, the NodeType
is defined as:
1type NodeType = 2 | AnyType 3 | NullType 4 | BooleanType 5 | StringType 6 | NumberType 7 | IntegerType 8 | ObjectType 9 | ArrayType 10 | TupleType 11 | RefType 12 | AndType 13 | OrType;
These types have an optional name
(string) property which can be converted to be required using NamedType<T = NodeType>
. This is useful when converting to other type systems where at least the top-most types must have names (like JSON Schema definitions or exported TypeScript types/interfaces), and is used by the NodeDocument
, which is what conversion packages should use:
1interface NodeDocument 2{ 3 version: 1; // core-types only has version 1 so far 4 types: Array< NamedType >; 5}
The types also have optional annotation properties title
(string), description
(string), examples
(string or array of strings), default
(string), see
(string or array of strings) and comment
(string).
All types except NullType
, AndType
and OrType
can have two properties const
(of type T
) or enum
(of type Array<T>
). The T
depends on the NodeType
. These have the same semantics as in JSON Schema, meaning a const
value is equivalent of an enum
with only that value. The enum
can be seen as a type literal union in TypeScript.
The AnyType
matches any type. Its const
and enum
properties have the element type T
set to unknown
.
This corresponds to any
or unknown
in TypeScript, and the empty schema {}
in JSON Schema.
Example: { type: 'any' }
The NullType
is simply equivalent to the TypeScript, JavaScript and JSON type null
.
Example: { type: 'null' }
The BooleanType
is equivalent to the TypeScript, JavaScript and JSON Boolean
(true
and false
).
The element type T
for const
and enum
is boolean
.
Example: { type: 'boolean', const: false }
The StringType
is equivalent to the TypeScript, JavaScript and JSON type String
.
The element type T
for const
and enum
is string
.
Example: { type: 'string', enum: [ "foo", "bar" ] }
core-types distinguishes between NumberType
and IntegerType
.
In TypeScript, JavaScript and JSON they are both equivalent to Number
. In JSON Schema however, integer
is a separate type, and can therefore be converted to core-types
with maintained type information.
The element type T
for const
and enum
is number
.
Example: { type: 'number', enum: [ 17, 42 ] }
The ObjectType
is used to describe the TypeScript type Record<string, NodeType>
and the JavaScript and JSON type Object
. In TypeScript or JavaScript, the keys must only be strings, not numbers or symbols.
The element type T
for const
and enum
is Record<string, any-json-type>
, i.e. plain objects.
Two more properties are required for an ObjectType
, properties
and additionalProperties
.
properties
is defined as Record<string, { node: NodeType; required: boolean; }>
.
additionalProperties
is defined as boolean | NodeType
. When this is false
, no additional properties apart from those defined in properties
are allowed, and if true
properties are allowed of any type (AnyType
). Otherwise additional properties are allowed of the defined NodeType
.
Example:
1{ 2 type: 'object', 3 properties: { 4 name: { node: { type: 'string' }, required: true }, 5 age: { node: { type: 'number' }, required: true }, 6 level: { node: { type: 'string', enum: [ 'novice', 'proficient', 'expert' ] }, required: false }, 7 }, 8 additionalProperties: false, 9}
The ArrayType
is used to describe the TypeScript type Array<NodeType>
and the JavaScript and JSON type Array
.
The element type T
for const
and enum
is Array<any-json-type>
, i.e. arrays of JSON-compatible types defined by the NodeType
in elementType
.
The extra and required property elementType
is of type NodeType
and defines what types the array can hold.
Example:
1{ 2 type: 'array', 3 elementType: { type: 'string' }, 4}
The TupleType
describes specific-length arrays where each position has a specific type. It matches the tuple type [A, B, ...]
in TypeScript
and is an Array
in JavaScript and JSON.
The element type T
for const
and enum
is [...any-json-types]
, i.e. tuples of JSON-compatible types defined by the NodeType
in the required elementTypes
and additionalItems
.
The extra and required properties for TupleType
are elementTypes
, minItems
and additionalItems
.
elementTypes
is defined as [...NodeType]
and describes the valid types for each position in the tuple for the required and individually typed optional tuple elements.
minItems
is an integer (TypeScript/JavaScript number) defining the minimum required elements and must not be negative. If this is greater than the number of elementTypes
, although valid in core-types per se, some conversions will limit it to the size of elementTypes
.
additionalProperties
is used to describe optional extra elemenents. It is defined as boolean | NodeType
. When this is false
, no additional elements are allowed, and if true
elements are allowed of any type (AnyType
). Otherwise additional elemenets are allowed of the defined NodeType
.
Example:
1{ 2 type: 'tuple', 3 elementTypes: [ 4 { type: 'string' }, 5 { type: 'boolean' }, // Optional, because minItems is 1 6 ], 7 minItems: 1, 8 additionalItems: { type: 'number' }, 9}
The RefType
describes references to other named types. Exactly what this means is up to the implementation of the user of core-types, but it is recommended that a reference type in a list of NodeType
s refers to a named type within that list. This corresponds to TypeScript named types being referred to in the same file as in which the type is defined, or JSON Schema $ref
references only referring to #/definitions/*
types.
A RefType
has a required property ref
which is a string corresponding to the name of the reference.
Example:
1[ 2 { 3 name: 'User', 4 type: 'object', 5 properties: { 6 name: { node: { type: 'string' }, required: true }, 7 id: { node: { type: 'number' }, required: true }, 8 }, 9 additionalProperties: true, 10 }, 11 { 12 name: 'UserList', 13 type: 'array', 14 elementType: { type: 'ref', ref: 'User' }, 15 }, 16]
The OrType
describes a union of other types. This is equivalent to union types in TypeScript (e.g. number | string
) and anyOf
in JSON Schema.
An OrType
has a required property or
which is defined as Array<NodeType>
.
Example:
1{ 2 type: 'or', 3 or: [ 4 { type: 'string' }, 5 { type: 'number' }, 6 { type: 'ref', ref: 'IdType' }, // Defined somewhere... 7 }, 8}
The AndType
describes an intersection of other types. This is equivalent to intersection types in TypeScript (e.g. A & B
) and allOf
in JSON Schema.
An AndType
has a required property and
which is defined as Array<NodeType>
.
Example:
1[ 2 { 3 name: 'CommentWithId', 4 type: 'and', 5 and: [ 6 { type: 'ref', ref: 'Comment' }, 7 { type: 'ref', ref: 'WithId' }, 8 }, 9 }, 10 { 11 name: 'Comment', 12 type: 'object', 13 properties: { 14 line: { node: { type: 'string' }, required: true }, 15 user: { node: { type: 'ref', ref: 'User' }, required: true }, 16 }, 17 additionalProperties: false, 18 }, 19 { 20 name: 'WithId', 21 type: 'object', 22 properties: { 23 id: { node: { type: 'string' }, required: true }, 24 }, 25 additionalProperties: false, 26 }, 27]
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 0/27 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More