Einenlum.

This Week I Learned: 2022W46

Mon Nov 21 2022

Neovim - Lua

Since I changed my vim config to use lua almost everywhere I didn’t understand why I kept having a folder called ~ created in many working folders. Apart from being super dangerous (no, I didn’t try to remove it with rm -Rf ~ fortunately [PLEASEDONTDOTHAT]. I was happy to use my GUI for this), it was super weird.

I realized it’s because of this option in one of my lua config files:

vim.opt.backupdir = "~/.config/nvim/backup"

It was creating a whole ./~/.config/nvim/backup in my current directory.

What worked before with VimScript dosn’t work with lua anymore (the ~ folder is not transformed into my HOME directory).

You have to use this instead:

vim.opt.backupdir = os.getenv("HOME") .. "/.config/nvim/backup"

Found here.

Typescript

I learned this week the existence of the Readonly keyword in typescript. In Typescript (and Javascript), the const keyword only implies you cannot set another value to the same variable name.

const my_val = 2;
// Cannot redeclare block-scoped variable 'my_val'.(2451)
const my_val = 3;

But if the value itself is mutable, declaring it as const still allows you to mutate it.

const my_val = ['a', 'b', 'c'];

// This is okay
my_val.push('d');

If you want to make it immutable, you can use the Readonly utility type.

const my_val: Readonly<string[]> = ['a', 'b', 'c'];

// Property 'push' does not exist on type 'readonly string[]'.(2339)
my_val.push('d');

It’s nice, but Typescript still won’t treat your variable as a constant:

const my_val: Readonly<string[]> = ['a', 'b', 'c'];

// const letter: string
const letter = my_val[1];

If you want to do so, you can use as const:

const my_val = ['a', 'b', 'c'] as const;

// const letter: 'c'
const letter = my_val[1];

And the mutation is still not possible.

Typescript also has multiple very interesting utility types. Documentation here.

Since version 4.9, Typescript also has a new satisfies operator, which allows to typehint a variable (and checks it’s valid) while still know about its values. You can check this well written article explaining it.

Python - Linter

It seems like Rust starts to become the new standard for fast tools. Ruff is the new kid on the block when it comes to Python linters. Didn’t try it yet, but the benchmarks are astonishing.