Types¶
The type system defines every type the Viper runtime understands. Type is the
base class: it exposes the primitive types as static accessors (Type.INT64,
Type.STRING, …) and serves as the common supertype of the parameterized
container, algebraic, and user-defined types.
When to use: reach for types to describe schemas, to validate data, and to build parameterized containers such as vectors and maps before instantiating values against them.
Quick Start¶
const { Type, TypeVector, TypeMap, TypeOptional } = require('@digitalsubstrate/dsviper');
// Primitive types are static accessors on Type
const tInt = Type.INT64;
const tStr = Type.STRING;
tStr.representation(); // "string"
// Parameterized container types — construct with `new`
const tVec = new TypeVector(Type.STRING); // vector<string>
const tMap = new TypeMap(Type.STRING, Type.INT64);
tMap.representation(); // "map<string, int64>"
// Nested types compose freely
const tNested = new TypeMap(Type.STRING, new TypeVector(Type.FLOAT));
// Nullable type
const tOpt = new TypeOptional(Type.STRING);
tOpt.representation(); // "optional<string>"
A type is the schema half of the runtime; pair it with Value.create(type, native)
to mint an instance (see Values):
const { Value, Type } = require('@digitalsubstrate/dsviper');
const v = Value.create(Type.STRING, 'hi'); // a ValueString
v.encoded(); // "hi"
Choosing the Right Type¶
Use Case |
Type Class |
Example |
|---|---|---|
Primitive value |
|
static accessors |
Homogeneous list |
|
|
Key-value mapping |
|
|
Nullable value |
|
|
Concurrent list |
|
|
Fixed-size tuple |
|
The mirror of this page for the Python binding is Type System; for a narrative walkthrough of types and values together, see Types and Values.
Generated from the @digitalsubstrate/dsviper TypeScript declarations (index.d.ts) by TypeDoc.
Summary¶
Class |
Description |
|---|---|
A utility class to handle type representation |
|
A class used to represent the any type |
|
A class used to represent the any_concept type |
|
A class used to represent the blob type |
|
A class used to represent the blob_id type |
|
A class used to represent the bool type |
|
A class used to represent a club type |
|
A class used to represent the commit_id type |
|
A class used to represent a concept type |
|
A class used to represent the double type |
|
A class used to represent an enum type |
|
A class used to represent a case for an enum type |
|
A class used to describe the cases of an enum |
|
A class used to represent the float type |
|
A class used to represent the int16 type |
|
A class used to represent the int32 type |
|
A class used to represent the int64 type |
|
A class used to represent the int8 type |
|
A class used to represent a key<element_type> type where element_type is a concept, club or any_concept |
|
A class used to represent a map<element_type> type |
|
A class used to represent a mat<numeric_type, columns, rows> type |
|
A class used to describe the name of a type |
|
A class used to represent an optional<element_type> type |
|
A class used to represent a set<element_type> type |
|
A class used to represent the string type |
|
A class used to represent a struct type |
|
A class used to describe the fields of a struct |
|
A class used to represent a field for a struct |
|
A class used to represent a tuple<T0, …> type |
|
A class used to represent the uint16 type |
|
A class used to represent the uint32 type |
|
A class used to represent the uint64 type |
|
A class used to represent the uint8 type |
|
A class used to represent the uuid type |
|
A class used to represent a variant<T0, …> type |
|
A class used to represent a vec<element_type, size> type |
|
A class used to represent a vector<element_type> type |
|
A class used to represent the void type |
|
A class used to represent a xarray<element_type> type |
Reference¶
- class Type()¶
A utility class to handle type representation.
Note: Not directly instantiable.
exported from
index.d- Type.ANY¶
type: readonly TypeAny
- Type.ANY_CONCEPT¶
type: readonly TypeAnyConcept
- Type.BLOB¶
type: readonly TypeBlob
- Type.BLOB_ID¶
type: readonly TypeBlobId
- Type.BOOL¶
type: readonly TypeBool
- Type.COMMIT_ID¶
type: readonly TypeCommitId
- Type.DOUBLE¶
type: readonly TypeDouble
- Type.FLOAT¶
type: readonly TypeFloat
- Type.INT16¶
type: readonly TypeInt16
- Type.INT32¶
type: readonly TypeInt32
- Type.INT64¶
type: readonly TypeInt64
- Type.INT8¶
type: readonly TypeInt8
- Type.STRING¶
type: readonly TypeString
- Type.UINT16¶
type: readonly TypeUInt16
- Type.UINT32¶
type: readonly TypeUInt32
- Type.UINT64¶
type: readonly TypeUInt64
- Type.UINT8¶
type: readonly TypeUInt8
- Type.UUID¶
type: readonly TypeUUId
- Type.VOID¶
type: readonly TypeVoid
- Type.compare(other)¶
- Arguments:
other (Type)
- Returns:
number
- Type.description(namespace)¶
- Arguments:
namespace (NameSpace)
- Returns:
string
- Type.equals(other)¶
- Arguments:
other (unknown)
- Returns:
boolean
- Type.representation(namespace)¶
- Arguments:
namespace (NameSpace)
- Returns:
string
- Type.runtimeId()¶
- Returns:
ValueUUId
- Type.toString()¶
- Returns:
string
- Type.typeCode()¶
- Returns:
string
- static Type.decode(blob, definitions, streamCodecInstancing)¶
- Return a type by decoding the blob with a StreamBinaryCodec if not
specified.
- Arguments:
blob (ValueBlob)
definitions (DefinitionsConst)
streamCodecInstancing (StreamCodecInstancing)
- Returns:
Type
- static Type.encode(type, streamCodecInstancing)¶
- Return a blob that encodes the type with a StreamBinaryCodec if not
specified.
- Arguments:
type (Type)
streamCodecInstancing (StreamCodecInstancing)
- Returns:
ValueBlob
- static Type.hexdigest(type, hashing)¶
- Return the hexa digest of the type computed with the hashing interface
if specified else use SHA1.
- Arguments:
type (Type)
hashing (Hashing)
- Returns:
string
- static Type.isSized(type)¶
Return true if the type has a fixed size.
- Arguments:
type (Type)
- Returns:
boolean
- static Type.read(streamReading, definitions)¶
Read and return a type.
- Arguments:
streamReading (StreamReading)
definitions (DefinitionsConst)
- Returns:
Type
- static Type.sizeOf(type, streamSizing)¶
Return the fixed size of the type or throw.
- Arguments:
type (Type)
streamSizing (StreamSizing)
- Returns:
number
- static Type.useBlobId(type)¶
Return true if the type uses the type blob_id.
- Arguments:
type (Type)
- Returns:
boolean
- static Type.write(type, streamWriting)¶
Write a type.
- Arguments:
type (Type)
streamWriting (StreamWriting)
- class TypeAny()¶
A class used to represent the any type.
Use the static property Type.ANY.
exported from
index.d- Extends:
Type
- TypeAny.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeAnyConcept()¶
A class used to represent the any_concept type.
Use the static property Type.ANY_CONCEPT.
exported from
index.d- Extends:
Type
- TypeAnyConcept.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeBlob()¶
A class used to represent the blob type.
Use the static property Type.BLOB.
exported from
index.d- Extends:
Type
- TypeBlob.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeBlobId()¶
A class used to represent the blob_id type.
Use the static property Type.BLOB_ID.
exported from
index.d- Extends:
Type
- TypeBlobId.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeBool()¶
A class used to represent the bool type.
Use the static property Type.BOOL.
exported from
index.d- Extends:
Type
- TypeBool.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeClub()¶
A class used to represent a club type.
Note: Not directly instantiable.
exported from
index.d- Extends:
Type
- TypeClub.documentation()¶
Return the documentation.
- Returns:
string
- TypeClub.isMember(typeConcept)¶
Return true type_concept is a member.
- Arguments:
typeConcept (TypeConcept)
- Returns:
boolean
- TypeClub.members()¶
Return the list of concepts.
- Returns:
TypeConcept[]
- TypeClub.typeName()¶
Return a TypeName.
- Returns:
TypeName
- static TypeClub.cast(type)¶
Return a type club or throw.
- Arguments:
type (Type)
- Returns:
TypeClub
- class TypeCommitId()¶
A class used to represent the commit_id type.
Use the static property Type.COMMIT_ID.
exported from
index.d- Extends:
Type
- TypeCommitId.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeConcept()¶
A class used to represent a concept type.
Note: Not directly instantiable.
exported from
index.d- Extends:
Type
- TypeConcept.documentation()¶
Return the documentation.
- Returns:
string
- TypeConcept.isMember(typeConcept)¶
Return true if type_concept is related.
- Arguments:
typeConcept (TypeConcept)
- Returns:
boolean
- TypeConcept.parent()¶
Return the parent concept or null.
- Returns:
TypeConcept | undefined
- TypeConcept.typeName()¶
Return a TypeName.
- Returns:
TypeName
- static TypeConcept.cast(type)¶
Return a concept or throw.
- Arguments:
type (Type)
- Returns:
TypeConcept
- class TypeDouble()¶
A class used to represent the double type.
Use the static property Type.DOUBLE.
exported from
index.d- Extends:
Type
- TypeDouble.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeEnumeration()¶
A class used to represent an enum type.
Note: Not directly instantiable.
exported from
index.d- Extends:
Type
- TypeEnumeration.cases()¶
Return the list of cases.
- Returns:
TypeEnumerationCase[]
- TypeEnumeration.check(name)¶
Return the case or throw.
- Arguments:
name (string)
- Returns:
TypeEnumerationCase
- TypeEnumeration.documentation()¶
Return the documentation.
- Returns:
string
- TypeEnumeration.query(name)¶
Return the case or null.
- Arguments:
name (string)
- Returns:
TypeEnumerationCase | undefined
- TypeEnumeration.typeName()¶
Return a TypeName.
- Returns:
TypeName
- static TypeEnumeration.cast(type)¶
Return a type enum or throw.
- Arguments:
type (Type)
- Returns:
TypeEnumeration
- class TypeEnumerationCase()¶
A class used to represent a case for an enum type.
Note: Not directly instantiable.
exported from
index.d- TypeEnumerationCase.documentation()¶
Return the documentation.
- Returns:
string
- TypeEnumerationCase.name()¶
Return the name.
- Returns:
string
- class TypeEnumerationDescriptor(name, documentation)¶
A class used to describe the cases of an enum.
exported from
index.d- Arguments:
name (string)
documentation (string)
- TypeEnumerationDescriptor.addCase(name, documentation)¶
Add a case to the enum.
- Arguments:
name (string)
documentation (string)
- TypeEnumerationDescriptor.cases()¶
Return the list of cases.
- Returns:
TypeEnumerationCase[]
- TypeEnumerationDescriptor.description()¶
Return the description.
- Returns:
string
- TypeEnumerationDescriptor.documentation()¶
Return the documentation.
- Returns:
string
- TypeEnumerationDescriptor.name()¶
Return the name.
- Returns:
string
- class TypeFloat()¶
A class used to represent the float type.
Use the static property Type.FLOAT.
exported from
index.d- Extends:
Type
- TypeFloat.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeInt16()¶
A class used to represent the int16 type.
Use the static property Type.INT16.
exported from
index.d- Extends:
Type
- TypeInt16.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeInt32()¶
A class used to represent the int32 type.
Use the static property Type.INT32.
exported from
index.d- Extends:
Type
- TypeInt32.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeInt64()¶
A class used to represent the int64 type.
Use the static property Type.INT64.
exported from
index.d- Extends:
Type
- TypeInt64.typeName()¶
Return A TypeName.
- Returns:
TypeName
- class TypeInt8()¶
A class used to represent the int8 type.
Use the static property Type.INT8.
exported from
index.d- Extends:
Type
- TypeInt8.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeKey(elementType)¶
- A class used to represent a key<element_type> type where element_type
is a concept, club or any_concept.
exported from
index.d- Extends:
Type
- Arguments:
elementType (Type)
- TypeKey.elementType()¶
Return the element type.
- Returns:
Type
- TypeKey.isAnyConcept()¶
Return true if element_type is any_concept.
- Returns:
boolean
- TypeKey.isClub()¶
Return true if element_type is a club.
- Returns:
boolean
- TypeKey.isConcept()¶
Return true if element_type is a concept.
- Returns:
boolean
- static TypeKey.cast(type)¶
Return the type key<element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeKey
- class TypeMap(keyType, elementType)¶
A class used to represent a map<element_type> type.
exported from
index.d- Extends:
Type
- Arguments:
keyType (Type)
elementType (Type)
- TypeMap.elementType()¶
Return the element type.
- Returns:
Type
- TypeMap.itemType()¶
Return the type for item.
- Returns:
TypeTuple
- TypeMap.itemsType()¶
Return the type for items.
- Returns:
TypeVector
- TypeMap.keyType()¶
Return the KeyType.
- Returns:
Type
- TypeMap.keysType()¶
Return the type for keys.
- Returns:
TypeSet
- TypeMap.valuesType()¶
Return the type for values.
- Returns:
TypeVector
- static TypeMap.cast(type)¶
Return the type map<key_type, element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeMap
- class TypeMat(elementType, columns, rows)¶
A class used to represent a mat<numeric_type, columns, rows> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (NumericType)
columns (number)
rows (number)
- TypeMat.columns()¶
Return the number of columns.
- Returns:
number
- TypeMat.elementType()¶
Return the element type.
- Returns:
NumericType
- TypeMat.elementsType()¶
Return the type for elements.
- Returns:
TypeVector
- TypeMat.rows()¶
Return the number of rows.
- Returns:
number
- static TypeMat.cast(type)¶
Return the type mat<N, columns, rows> or throw.
- Arguments:
type (Type)
- Returns:
TypeMat
- class TypeName()¶
A class used to describe the name of a type.
Note: Not directly instantiable.
exported from
index.d- TypeName.name()¶
Return the name.
- Returns:
string
- TypeName.nameSpace()¶
Return the namespace.
- Returns:
NameSpace
- class TypeOptional(elementType)¶
A class used to represent an optional<element_type> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (Type)
- TypeOptional.elementType()¶
Return the element type.
- Returns:
Type
- static TypeOptional.cast(type)¶
Return the type optional<element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeOptional
- class TypeSet(elementType)¶
A class used to represent a set<element_type> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (Type)
- TypeSet.elementType()¶
Return the element type.
- Returns:
Type
- TypeSet.elementsType()¶
Return the type for elements.
- Returns:
TypeVector
- static TypeSet.cast(type)¶
Return the type set<element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeSet
- class TypeString()¶
A class used to represent the string type.
Use the static property Type.STRING.
exported from
index.d- Extends:
Type
- TypeString.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeStructure()¶
A class used to represent a struct type.
Note: Not directly instantiable.
exported from
index.d- Extends:
Type
- TypeStructure.check(fieldName)¶
Return the field or throw.
- Arguments:
fieldName (string)
- Returns:
TypeStructureField
- TypeStructure.documentation()¶
Return The documentation.
- Returns:
string
- TypeStructure.fields()¶
Return the list of fields.
- Returns:
TypeStructureField[]
- TypeStructure.isCompact()¶
Return true the type is compact.
- Returns:
boolean
- TypeStructure.query(fieldName)¶
Return the field or null.
- Arguments:
fieldName (string)
- Returns:
TypeStructureField | undefined
- TypeStructure.typeName()¶
Return a TypeName.
- Returns:
TypeName
- static TypeStructure.cast(type)¶
Return a type struct or throw.
- Arguments:
type (Type)
- Returns:
TypeStructure
- class TypeStructureDescriptor(name, documentation)¶
A class used to describe the fields of a struct.
exported from
index.d- Arguments:
name (string)
documentation (string)
- TypeStructureDescriptor.addField(fieldName, type, documentation)¶
Add a field where the type is specified or deducted from the value.
- Arguments:
fieldName (string)
type (Type)
documentation (string)
- TypeStructureDescriptor.description()¶
Return the description.
- Returns:
string
- TypeStructureDescriptor.documentation()¶
Return the documentation.
- Returns:
string
- TypeStructureDescriptor.fields()¶
Return the list of fields.
- Returns:
TypeStructureField[]
- TypeStructureDescriptor.name()¶
Return the name.
- Returns:
string
- class TypeStructureField()¶
A class used to represent a field for a struct.
Note: Not directly instantiable.
exported from
index.d- TypeStructureField.defaultValue()¶
Return the default value or null.
- Returns:
Value | undefined
- TypeStructureField.documentation()¶
Return the documentation.
- Returns:
string
- TypeStructureField.name()¶
Return the name.
- Returns:
string
- TypeStructureField.type()¶
Return the type.
- Returns:
Type
- class TypeTuple(types)¶
A class used to represent a tuple<T0, …> type.
exported from
index.d- Extends:
Type
- Arguments:
types (Type[])
- TypeTuple.types()¶
Return the list of types.
- Returns:
Type[]
- static TypeTuple.cast(type)¶
Return a type tuple<T0, …> or throw.
- Arguments:
type (Type)
- Returns:
TypeTuple
- class TypeUInt16()¶
A class used to represent the uint16 type.
Use the static property Type.UINT16.
exported from
index.d- Extends:
Type
- TypeUInt16.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeUInt32()¶
A class used to represent the uint32 type.
Use the static property Type.UINT32.
exported from
index.d- Extends:
Type
- TypeUInt32.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeUInt64()¶
A class used to represent the uint64 type.
Use the static property Type.UINT64.
exported from
index.d- Extends:
Type
- TypeUInt64.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeUInt8()¶
A class used to represent the uint8 type.
Use the static property Type.UINT8.
exported from
index.d- Extends:
Type
- TypeUInt8.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeUUId()¶
A class used to represent the uuid type.
Use the static property Type.UUID.
exported from
index.d- Extends:
Type
- TypeUUId.typeName()¶
Return A TypeName.
- Returns:
TypeName
- class TypeVariant(types)¶
A class used to represent a variant<T0, …> type.
exported from
index.d- Extends:
Type
- Arguments:
types (Type[])
- TypeVariant.types()¶
Return the list of types.
- Returns:
Type[]
- static TypeVariant.cast(type)¶
Return a type variant<T0, …> or throw.
- Arguments:
type (Type)
- Returns:
TypeVariant
- class TypeVec(elementType, size)¶
A class used to represent a vec<element_type, size> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (NumericType)
size (number)
- TypeVec.elementType()¶
Return the element type.
- Returns:
NumericType
- TypeVec.elementsType()¶
Return the type for elements.
- Returns:
TypeVector
- TypeVec.size()¶
Return the number of elements.
- Returns:
number
- static TypeVec.cast(type)¶
Return a vec<N, size> or throw.
- Arguments:
type (Type)
- Returns:
TypeVec
- class TypeVector(elementType)¶
A class used to represent a vector<element_type> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (Type)
- TypeVector.elementType()¶
Return the element type.
- Returns:
Type
- static TypeVector.cast(type)¶
Return a type vector<element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeOptional
- class TypeVoid()¶
A class used to represent the void type.
Use the static property Type.VOID.
exported from
index.d- Extends:
Type
- TypeVoid.typeName()¶
Return a TypeName.
- Returns:
TypeName
- class TypeXArray(elementType)¶
A class used to represent a xarray<element_type> type.
exported from
index.d- Extends:
Type
- Arguments:
elementType (Type)
- TypeXArray.elementType()¶
Return the type.
- Returns:
Type
- TypeXArray.elementsType()¶
Return the type for elements.
- Returns:
TypeVector
- static TypeXArray.cast(type)¶
Return a type xarray<element_type> or throw.
- Arguments:
type (Type)
- Returns:
TypeXArray