TypeScript type definitions for CLR/.NET runtime primitives
npm install @tsonic/typesTypeScript type definitions for CLR/.NET primitives.
``bash`
npm install @tsonic/types
`typescript
import { int, decimal, bool, long } from "@tsonic/types";
const age: int = 42 as int;
const price: decimal = 99.99 as decimal;
const isActive: bool = true as bool;
const timestamp: long = Date.now() as long;
`
Add to your tsconfig.json:
`json`
{
"compilerOptions": {
"types": ["@tsonic/types/global"]
}
}
Or use a triple-slash reference:
`typescript
///
const count: int = 10 as int;
`
| Type | CLR Type | Range | Bytes |
|------|----------|-------|-------|
| sbyte | System.SByte | -128 to 127 | 1 |byte
| | System.Byte | 0 to 255 | 1 |short
| | System.Int16 | -32,768 to 32,767 | 2 |ushort
| | System.UInt16 | 0 to 65,535 | 2 |int
| | System.Int32 | -2,147,483,648 to 2,147,483,647 | 4 |uint
| | System.UInt32 | 0 to 4,294,967,295 | 4 |long
| | System.Int64 | ~-9.2e18 to ~9.2e18 | 8 |ulong
| | System.UInt64 | 0 to ~1.8e19 | 8 |nint
| | System.IntPtr | Platform-dependent | 4/8 |nuint
| | System.UIntPtr | Platform-dependent | 4/8 |int128
| | System.Int128 | 128-bit signed | 16 |uint128
| | System.UInt128 | 128-bit unsigned | 16 |
| Type | CLR Type | Precision | Bytes |
|------|----------|-----------|-------|
| half | System.Half | 16-bit float | 2 |float
| | System.Single | 32-bit float | 4 |double
| | System.Double | 64-bit float | 8 |decimal
| | System.Decimal | 128-bit decimal | 16 |
| Type | CLR Type | Description |
|------|----------|-------------|
| bool | System.Boolean | Boolean value |char
| | System.Char | Single UTF-16 code unit |
| Type | C# Modifier | Description |
|------|-------------|-------------|
| ref | ref | Reference parameter (read/write) |out
| | out | Output parameter (write-only) |In
| | in | Readonly reference parameter |
| Type | C# Type | Description |
|------|---------|-------------|
| ptr | T* | Unsafe pointer type |
These types use TypeScript's branded types pattern to provide type safety without runtime overhead:
`typescript`
export type int = number & { __brand: "int" };
The types are distinguishable at compile time but compile down to standard JavaScript primitives. There are no wrapper classes or runtime checks.
`typescript
import { int, long } from "@tsonic/types";
const age: int = 42 as int;
const timestamp: long = Date.now() as long;
// Type error: Type 'long' is not assignable to type 'int'
const invalid: int = timestamp;
// Explicit cast required
const valid: int = timestamp as unknown as int;
`
`typescript
import { int, decimal } from "@tsonic/types";
const scores: Array
const prices: Array
`
`typescript
import { int, bool } from "@tsonic/types";
function setAge(age: int): void {
console.log(Age set to ${age});
}
function isValid(value: int): bool {
return (value > 0) as bool;
}
setAge(25 as int);
const result: bool = isValid(10 as int);
`
`typescript
import { int, ref, out, In } from "@tsonic/types";
// ref parameter - can read and write
function swap(a: ref
const temp = a;
a = b;
b = temp;
}
// out parameter - must write before return
function tryParse(input: string, value: out
// Parser sets value through out parameter
return true as bool;
}
// in parameter - readonly reference
function process(data: In
// Can read data but not modify it
console.log(data);
}
`
`typescript
import { ptr, int } from "@tsonic/types";
// Pointer types require explicit handling
declare function getPointer(): ptr
declare function dereferenceInt(p: ptr
const pointer = getPointer();
// pointer is ptr
const value = dereferenceInt(pointer); // Explicit dereference required
``
MIT