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 value into an object type specified by Type Code or returns null when conversion is not possible.

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

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

toString

Converts a TypeCode into its string name.

public static toString(type: TypeCode): string

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

toType

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

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

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

toTypeCode

Gets TypeCode for specific value.

public static toTypeCode(value: any): TypeCode

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

toTypeWithDefault

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

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

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