<commit_before>---
layout: post
title: TypeScript tips from Kotlin developer - const
author: piotr
hidden: true
tags: TypeScript Kotlin const immutability
comments: true
crosspost: true
image: /images/kotlin/kotlin-logo.png
---

Nowadays I mostly code in [Kotlin programming language](https://kotlinlang.org/). I got interested in it when I started working on a Java 7 codebase and quickly got fed up with the language. At [Bright Inventions](https://brightinventions.pl) we often use TypeScript for both back-end, front-end and mobile development so I though I would share some thoughts and tips for TypeScript learned when using Kotlin. In particular this post is about constant variables.

## Use `const` whenever possible

[Using immutable variables](https://hackernoon.com/5-benefits-of-immutable-objects-worth-considering-for-your-next-project-f98e7e85b6ac) aids reasoning about flow and state of a program. It helps compiler to provide more intelligent hints especially when dealing with nullable types.

In Kotlin a `val` keyword denotes a variable which value does not change after initialization e.g.:

```kotlin
val x: Int
val y = 3
x = 2
x = 20 // Error: Val cannot be reassigned
y = 30 // Error: Val cannot be reassigned
```

In [TypeScript](https://www.typescriptlang.org/) such situation is handled with `const`:

```TypeScript
const x: number // Error: 'const' declarations must be initialized
const y: number = 3

y = 30 // Error: Cannot assign to 'y' because it is constant or read-only property
```

## Compilers love `const`

The first way in which compiler gets smarter when we use `const` is null checks. When you enable [`strictNullChecks`](https://www.typescriptlang.org/docs/handbook/compiler-options.html), which you should, both Kotlin and TypeScript compiler understand if something can or cannot be null.

```TypeScript
const firstName: string | null = getFirstName();
let lastName: string | null = getLastName();

if(firstName !== null && lastName !== null) {
    setTimeout(() => {
        console.log(firstName.length)
        console.log(lastName.length) // Error: Object is possibly 'null'
    })
}
```

## Compilers catch bugs with `const`

Because `const` and `val` can only be assigned once compilers can prevent another class of bugs. Take a look at the code example blow. There is a bug 🐛 that could easily be avoided had we used `const` instead of `let`.

```TypeScript
let firstName: string = person.firstName
let lastName: string = person.lastName<commit_msg>Fix langauge signature for code blocks<commit_after>---
layout: post
title: TypeScript tips from Kotlin developer - const
author: piotr
hidden: true
tags: TypeScript Kotlin const immutability
comments: true
crosspost: true
image: /images/kotlin/kotlin-logo.png
---

Nowadays I mostly code in [Kotlin programming language](https://kotlinlang.org/). I got interested in it when I started working on a Java 7 codebase and quickly got fed up with the language. At [Bright Inventions](https://brightinventions.pl) we often use TypeScript for both back-end, front-end and mobile development so I though I would share some thoughts and tips for TypeScript learned when using Kotlin. In particular this post is about constant variables.

## Use `const` whenever possible

[Using immutable variables](https://hackernoon.com/5-benefits-of-immutable-objects-worth-considering-for-your-next-project-f98e7e85b6ac) aids reasoning about flow and state of a program. It helps compiler to provide more intelligent hints especially when dealing with nullable types.

In Kotlin a `val` keyword denotes a variable which value does not change after initialization e.g.:

```kotlin
val x: Int
val y = 3
x = 2
x = 20 // Error: Val cannot be reassigned
y = 30 // Error: Val cannot be reassigned
```

In [TypeScript](https://www.typescriptlang.org/) such situation is handled with `const`:

```typescript
const x: number // Error: 'const' declarations must be initialized
const y: number = 3

y = 30 // Error: Cannot assign to 'y' because it is constant or read-only property
```

## Compilers love `const`

The first way in which compiler gets smarter when we use `const` is null checks. When you enable [`strictNullChecks`](https://www.typescriptlang.org/docs/handbook/compiler-options.html), which you should, both Kotlin and TypeScript compiler understand if something can or cannot be null.

```typescript
const firstName: string | null = getFirstName();
let lastName: string | null = getLastName();

if(firstName !== null && lastName !== null) {
    setTimeout(() => {
        console.log(firstName.length)
        console.log(lastName.length) // Error: Object is possibly 'null'
    })
}
```

## Compilers catch bugs with `const`

Because `const` and `val` can only be assigned once compilers can prevent another class of bugs. Take a look at the code example blow. There is a bug 🐛 that could easily be avoided had we used `const` instead of `let`.

```typescript
let firstName: string = person.firstName
let lastName: string = person.lastName
{'fraction_non_alphanumeric': 0.09190371991247265, 'fraction_numerical': 0.006763477222995823, 'mean_word_length': 4.371794871794871, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '980836', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1364, 'n_words': 640}