Nim for TypeScript Devs
Tags: nim, javascript, typescript
Published: 2020-01-01 21:12:03

---
title: Nim for TypeScript Devs
published: true
description: Nim vs TypeScript
tags: nim, javascript, typescript
---


Feature              |  TypeScript                          | Nim
---------------------|--------------------------------------|-----------------------------------------
Execution model      | JavaScript code (Compiler)           | JavaScript code (Compiler)
Written using        | TypeScript                           | Nim
License              | Apache                               | MIT
Version (Mayor)      | `3.x`                                | `1.x`
Typing               | Static                               | Static
Meta-programming     | :negative_squared_cross_mark: [#issue-13252](https://github.com/Microsoft/TypeScript/issues/13252#issuecomment-334646630), [Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) are limited | :heavy_check_mark: template, macro
int8/16/32/64 types  | :negative_squared_cross_mark:        | :heavy_check_mark:
float32/float64 types | :negative_squared_cross_mark:       | :heavy_check_mark:
Char types           | :negative_squared_cross_mark:        | :heavy_check_mark:
Subrange types       | :negative_squared_cross_mark: [#issue-225324972](https://github.com/microsoft/TypeScript/issues/15480#issue-225324972) | :heavy_check_mark:
JSON types           | :negative_squared_cross_mark: [#issue-56296923](https://github.com/microsoft/TypeScript/issues/1897#issue-56296923) | :heavy_check_mark:
Regex types          | :negative_squared_cross_mark: [#issue-128264906](https://github.com/Microsoft/TypeScript/issues/6579#issue-128264906) | :heavy_check_mark:
Option types         | :negative_squared_cross_mark:        | :heavy_check_mark:
Dependent types      | :negative_squared_cross_mark:        | :heavy_check_mark:
Operator Overloading | :negative_squared_cross_mark:        | :heavy_check_mark:
Custom Operators     | :negative_squared_cross_mark:        | :heavy_check_mark:
Enum types           | :heavy_check_mark:                   | :heavy_check_mark:
Immutability         | Limited, `readonly` keyword          | :heavy_check_mark:
Function Arguments Immutability | Mutable                   | Immutable
Full DOM API         | :heavy_check_mark:                   | :heavy_check_mark:  
NodeJS integration | :heavy_check_mark:                   | :heavy_check_mark:  
Generics             | :heavy_check_mark:                   | :heavy_check_mark:
Type inference       | :heavy_check_mark:                   | :heavy_check_mark: 
Closures             | :heavy_check_mark:                   | :heavy_check_mark:
Object-Oriented      | :heavy_check_mark:                   | :heavy_check_mark:
Methods              | :heavy_check_mark:                   | :heavy_check_mark:
Exceptions           | :heavy_check_mark:                   | :heavy_check_mark:
Anonymous Functions  | :heavy_check_mark:                   | :heavy_check_mark: 
Arrow Functions      | :heavy_check_mark:                   | :heavy_check_mark:
Array Comprehensions  | :heavy_check_mark:                  | :heavy_check_mark:
Formatted String Literals | :heavy_check_mark:              | :heavy_check_mark: 
FFI                  | :heavy_check_mark: JS only           | :heavy_check_mark: C/C++/JS
Async                | :heavy_check_mark:                   | :heavy_check_mark:
Regex                | :heavy_check_mark:                   | :heavy_check_mark:
Self-Documentation comments | :heavy_check_mark:            | :heavy_check_mark: 
Package Publishing   | :heavy_check_mark:                   | :heavy_check_mark: 
Package Manager      | :heavy_check_mark:                   | :heavy_check_mark: 
Code AutoFormatter   | :heavy_check_mark: via NPM           | :heavy_check_mark: Nimpretty
Syntax similar to    | JavaScript                           | CoffeeScript or Python

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/iokz2yvbof8krq0wh3sz.png)


# Variables

Creating a new variable uses `var` or `let` or `const`.
Nim has immutability and compile-time function execution.
You can assign functions to variables.

- `const` is different from TypeScript by being truly immutable.

| Declaration | Compile-Time | Run-Time | Immutable | Requires Assignment |
|-------------|--------------|----------|-----------|---------------------|
`var`         | :negative_squared_cross_mark: | :heavy_check_mark: | :negative_squared_cross_mark: |  :negative_squared_cross_mark: |
`let`         | :negative_squared_cross_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
`const`       | :heavy_check_mark: | :negative_squared_cross_mark: | :heavy_check_mark: | :heavy_check_mark: |


# Compile-Time Function Execution

Nim has Compile-Time Function Execution that allows you to run *Backend-like* code at compile-time and use it on Frontend at run-time.
Compile-time FFI is also possible, you can pull data from C/C++ libs at compile-time and use it on Frontend at run-time. 
Most code that works at compile-time and NimScript also tends to work for Frontend.

Example: [Compile-time recursive file system walk module.](https://nim-lang.github.io/Nim/oswalkdir.html)


# Svelte

Nim has some similarities with Svelte for Frontend, but at the time of writing Svelte does not fully supports TypeScript.
Svelte can not do Backend (is not designed to).

![](https://thepracticaldev.s3.amazonaws.com/i/t3oi35bp5v2ls0b3rrnk.png)

# Build Modes

Feature    | Release Build                 | Debug Build                   |
-----------|-------------------------------|-------------------------------|
Speed      | Fast                          | Slow                          
File Size  | Small                         | Big                           
Optimized  | :heavy_check_mark:            | :negative_squared_cross_mark: 
Tracebacks | :negative_squared_cross_mark: | :heavy_check_mark: 
Run-time checks | :negative_squared_cross_mark: | :heavy_check_mark: 
Compile-time checks | :heavy_check_mark:  | :heavy_check_mark: 
`assert`   | :negative_squared_cross_mark: | :heavy_check_mark: 
`doAssert` | :heavy_check_mark:  | :heavy_check_mark: 

- Once compiled it wont need Nim anymore, 100% dependency-free.


# Hello World

```nim
echo "Hello World"
```

**Console Hello World:**
```nim
console.log("Hello World")
```


# Thank you for playing

[![](https://thepracticaldev.s3.amazonaws.com/i/4huq62a5r8tck64apdtd.png)](https://nim-lang.org)

There's bridged [Gitter](https://gitter.im/nim-lang/Nim), [Matrix](https://matrix.to/#/#freenode_#nim:matrix.org), [Telegram](https://t.me/nim_lang), [IRC](irc://freenode.net/nim), [Discord](https://discord.gg/ezDFDw2) for Nim, come say *Hi*.

[Nim Homepage](https://nim-lang.org)
[Learn Nim today.](https://nim-lang.org/learn.html)
[Grupo de Telegram en Español.](https://t.me/NimArgentina)

👑