TypeConverter

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object.

See also TypeCode

Description

The TypeConverter class allows you to convert arbitrary values into objects specified by a code type and to get the code type of an object. The code types are defined in the TypeCode class.

Static methods

toNullableType

Converts a value into an object of the type specified by a TypeCode or returns null when the conversion is not possible.

public static toNullableType<T>(type: TypeCode, value: any): T

  • type: any - TypeCode for the data type into which ‘value’ is to be converted.
  • value: any - value to convert.
  • returns: T - object value of type corresponding to TypeCode, or null when the conversion is not supported.

toString

Converts a TypeCode into its string name.

public static toString(type: TypeCode): string

  • type: TypeCode - TypeCode to convert into a string.
  • returns: string - name of the TypeCode passed as a string value.

toType

Converts a value into an object of the type specified by Type Code or returns an object of default type when the conversion is not possible.

public static toType<T>(type: TypeCode, value: any): T

  • type: TypeCode - type to which the value is to be converted into.
  • value: any - value to convert.
  • returns: T - object value of type corresponding to TypeCode, or object of default type when the conversion is not supported.

toTypeCode

Gets a TypeCode for a specific value.

public static toTypeCode(value: any): TypeCode

  • value: any - value whose TypeCode is to be resolved.
  • returns: TypeCode - TypeCode that corresponds to the passed object’s type.

toTypeWithDefault

Converts a value into an object of the type specified by Type Code or returns a default value when the conversion is not possible.

public static toTypeWithDefault<T>(type: TypeCode, value: any, defaultValue: T): T

  • type: TypeCode - TypeCode for the data type into which ‘value’ is to be converted.
  • value: any - value to convert.
  • defaultValue: T - default value to return if conversion is not possible (returns null).
  • returns: T - object value of type corresponding to TypeCode, or default value when the conversion is not supported.

Examples

let value1 = TypeConverter.toType(TypeCode.Integer, "123.456"); // Result: 123
let value2 = TypeConverter.toType(TypeCode.DateTime, 123); // Result: Date(123)
let value3 = TypeConverter.toType(TypeCode.Boolean, "F"); // Result: false

See also