Skip to content

Sefrancois/nullable

Repository files navigation

@sefr/nullable ✨

What does it do ? 💡

Provide a simple implementation of the Nullable<T> type which allow a value to be either T or null.

Compatibility 🔧

TypeScript EcmaScript
>= 2.8.0 >= ES2015

Dependencies 🎱

This package is dependencies-free.

Installation 💾

Nothing more than :

npm i -S @sefr/nullable

How to use 📚

✅ Returning a success without content :

import { Nullable } from "@sefr/nullable";

function doSomething(condition: boolean): Nullable<string> {
    if (condition) {
		return "toto";
    } else {
		return null;
    }
}

const toto: Nullable<string> = doSomething(true);
console.log(toto); // "toto"
const titi: Nullable<string> = doSomething(false);
console.log(titi); // null

Incorrect usages ❌

❌ Returning undefined instead of null won't work at compilation time :

import { Nullable } from "@sefr/nullable";

function doSomething(condition: boolean): Nullable<string> {
	if (condition) {
		return "toto";
	} else {
		return undefined;
	}
}

Credits 📎

License 📜

This software is available under the MIT License. See the LICENSE file for more informations.

⬆️ Go back to top