Code Samples: TypeScript to C
January 26, 2026 ยท View on GitHub
This document shows TypeScript code samples alongside their conceptual C# equivalents. Use this to understand what SharpTS can do and how TypeScript constructs map to .NET.
Note: The C# code shown is conceptual - it represents what the compiled code does, not the literal IL output. Actual compiled assemblies use runtime wrappers for dynamic behavior.
Table of Contents
- Basic Types and Variables
- Functions
- Classes
- Interfaces
- Enums
- Arrays and Collections
- Control Flow
- Advanced Features
- Modules
- Decorators
- Type Mapping Reference
Basic Types and Variables
Primitive Types
| TypeScript | C# Equivalent |
|---|---|
|
|
Type Inference
| TypeScript | C# Equivalent |
|---|---|
|
|
Constants
| TypeScript | C# Equivalent |
|---|---|
|
|
Functions
Basic Functions
| TypeScript | C# Equivalent |
|---|---|
|
|
Arrow Functions
| TypeScript | C# Equivalent |
|---|---|
|
|
Default Parameters
| TypeScript | C# Equivalent |
|---|---|
|
|
Rest Parameters
| TypeScript | C# Equivalent |
|---|---|
|
|
Closures
| TypeScript | C# Equivalent |
|---|---|
|
|
Classes
Basic Class
| TypeScript | C# Equivalent |
|---|---|
|
|
Inheritance
| TypeScript | C# Equivalent |
|---|---|
|
|
Getters and Setters
| TypeScript | C# Equivalent |
|---|---|
|
|
Static Members
| TypeScript | C# Equivalent |
|---|---|
|
|
Abstract Classes
| TypeScript | C# Equivalent |
|---|---|
|
|
Interfaces
Basic Interface
| TypeScript | C# Equivalent |
|---|---|
|
|
Class Implementing Interface
| TypeScript | C# Equivalent |
|---|---|
|
|
Enums
Numeric Enums
| TypeScript | C# Equivalent |
|---|---|
|
|
Enums with Custom Values
| TypeScript | C# Equivalent |
|---|---|
|
|
String Enums
| TypeScript | C# Equivalent |
|---|---|
|
|
Arrays and Collections
Array Basics
| TypeScript | C# Equivalent |
|---|---|
|
|
Array Methods
| TypeScript | C# Equivalent |
|---|---|
|
|
Map
| TypeScript | C# Equivalent |
|---|---|
|
|
Set
| TypeScript | C# Equivalent |
|---|---|
|
|
Object Literals
| TypeScript | C# Equivalent |
|---|---|
|
|
Control Flow
Conditionals
| TypeScript | C# Equivalent |
|---|---|
|
|
Switch Statement
| TypeScript | C# Equivalent |
|---|---|
|
|
Loops
| TypeScript | C# Equivalent |
|---|---|
|
|
Try/Catch/Finally
| TypeScript | C# Equivalent |
|---|---|
|
|
Advanced Features
Async/Await
| TypeScript | C# Equivalent |
|---|---|
|
|
Generators
| TypeScript | C# Equivalent |
|---|---|
|
|
Template Literals
| TypeScript | C# Equivalent |
|---|---|
|
|
Destructuring
| TypeScript | C# Equivalent |
|---|---|
|
|
Spread Operator
| TypeScript | C# Equivalent |
|---|---|
|
|
Optional Chaining
| TypeScript | C# Equivalent |
|---|---|
|
|
Nullish Coalescing
| TypeScript | C# Equivalent |
|---|---|
|
|
Generics
| TypeScript | C# Equivalent |
|---|---|
|
|
Modules
Named Exports/Imports
| TypeScript (math.ts) | C# Equivalent |
|---|---|
|
|
| TypeScript (main.ts) | C# Equivalent |
|---|---|
|
|
Default Exports
| TypeScript (greeter.ts) | C# Equivalent |
|---|---|
|
|
| TypeScript (main.ts) | C# Equivalent |
|---|---|
|
|
Decorators
Note: Decorators are enabled by default (Stage 3). Use
--experimentalDecoratorsfor Legacy (Stage 2) decorators.
Class Decorator
| TypeScript | C# Equivalent (Conceptual) |
|---|---|
|
|
Method Decorator
| TypeScript | C# Equivalent (Conceptual) |
|---|---|
|
|
Type Mapping Reference
| TypeScript | .NET Type | Notes |
|---|---|---|
number | double | All numbers are 64-bit floats |
string | string | Direct mapping |
boolean | bool | Direct mapping |
null | null | Represented as null object |
undefined | null | Treated as null at runtime |
any | object | Dynamic typing |
unknown | object | Requires type checking |
void | void | No return value |
never | void | Function never returns |
bigint | BigInteger | Arbitrary precision |
symbol | Custom | Runtime symbol type |
Array<T> | object[] | Runtime array wrapper |
T[] | object[] | Same as Array<T> |
Promise<T> | Task<T> | Async operations |
Map<K,V> | Dictionary<K,V> | Key-value collection |
Set<T> | HashSet<T> | Unique values |
Date | DateTime | Date/time operations |
RegExp | Regex | Regular expressions |
| Object literal | Dictionary<string,object> | Dynamic properties |
| Class | Generated class | IL class definition |
| Interface | Compile-time only | Structural typing |
| Enum | Generated enum | With reverse mapping |
| Union types | object | Runtime type checking |
| Function | TSFunction | Callable wrapper |
What's Not Supported
SharpTS focuses on core TypeScript features. The following are not currently supported:
- Ambient declarations (
.d.tsfiles) - JSX/TSX
- Decorators on parameters (class and method decorators only)
eval()and dynamic code execution- Prototype manipulation
withstatement
Running the Examples
Interpreted mode (development):
dotnet run -- example.ts
Compiled mode (production):
dotnet run -- --compile example.ts
dotnet example.dll
See Execution Modes for more details on when to use each mode.