Variant types in Roblox TypeScript - Ported from Vanilla TypeScript

Overview

Variant (for Roblox)

This is a roblox typescript variant (heh, pun) of Variant. See the Variant documentation on how to use Variant.

A variant type is like an enum but each case can hold some extra data.

npm i --save @rbxts/variant

As per the original repo description - Variant aims to bring the experience of variant types to TypeScript. Variant types, a.k.a. discriminated unions in the TypeScript world, are an excellent tool for describing and handling flexible domain models and tiny DSLs. However, because "TypeScript instead builds on JavaScript patterns as they exist today" using them as-is can result in tedious and fragile code. This project addresses that by providing well-typed, fluent, and expressive tools to safely do away with the boilerplate.

Documentation

Example usage

import { variantModule, TypeNames, VariantOf, fields, match } from "@rbxts/variant";

export const Animal = variantModule({
    dog: fields<{name: string, favoriteBall?: string}>(),
    cat: fields<{name: string, furnitureDamaged: number}>(),
    snake: (name: string, pattern = 'striped') => ({name, pattern}),
});

export type Animal<T extends TypeNames<typeof Animal> = undefined>
    = VariantOf<typeof Animal, T>;

const aDog = Animal.dog({name: "Wally"}); // will result in a discriminate value of { type: "dog", name: "Wally" }
const aCat = Animal.cat({name: "Whiskers", furnitureDamaged: true}); // will result in a discriminate value of { type: "cat", name: "Whiskers", 
const aSnake = Animal.snake("Nyx", "dotted")

const describeAnimal = (animal: Animal) => match(animal, {
    cat: ({name}) => `${name} is sleeping on a sunlit window sill.`,
    dog: ({name, favoriteBall}) => [
        `${name} is on the rug`,
        favoriteBall ? `nuzzling a ${favoriteBall} ball.` : '.' 
    ].join(' '),
    snake: s => `${s.name} is enjoying the heat of the lamp on his ${s.pattern} skin`,
});

print(describeAnimal(aDog)) //  Wally is on the rug
print(describeAnimal(aCat)) // Whiskers is sleeping on a sunlit window sill.
print(describeAnimal(aSnake)) // Nyx is enjoying the heat of the lamp on his spotted skin

Supports

Functions

  • cast
  • isOfVariant
  • keys
  • keymap
  • lookup
  • match
  • matchElse (deprecated in main lib, so not included)
  • narrow
  • outputTypes
  • partialLookup
  • partialMatch (deprecated in main lib, so not included)

Variant Creation

  • variant
  • fields
  • payload
  • variantFactory
  • variantList
  • variantModule

Types

  • Flags<T>
  • Handler<T>
  • KeysOf<T>
  • Lookup<T, U>
  • Matrix<T>
  • TypeNames<T>
  • VariantCreator<T, F, K?>
  • VariantModule<K>
  • VariantOf<T>
  • TypeExt<K, T>
  • WithProperty<K, T> (deprecated in main lib, so not included)

Credits

Thanks to Paarth for creating this awesome library for vanilla TypeScript, without their awesome work this would not exist.

You might also like...

A free and open-source Roblox script hub

🐋 Orca A free and open-source Roblox script hub 🐋 Introduction Orca is a general-purpose Roblox script hub designed to make convenient tasks easy an

Dec 27, 2022

Downloads & formats all of your Roblox private messages.

Downloads & formats all of your Roblox private messages.

Roblox Message Downloader This tool was created due to the recent news sent out by Roblox. On April 22nd, 2022, all private messages sent by Roblox be

Apr 7, 2022

logs ROBLOX's updates and new versions

logs ROBLOX's updates and new versions

roblox-update-notifier logs ROBLOX's updates and new versions This is meant to be ran in NodeJS, 24/7, using something like pm2. NPM packages required

Oct 23, 2022

RWP stands for Roblox web panel, it's a code snippet that you can run via developer console or the provided Google Chrome extension to try out early

RWP stands for Roblox web panel, it's a code snippet that you can run via developer console or the provided Google Chrome extension to try out early

RWP stands for Roblox web panel, it's a code snippet that you can run via developer console or the provided Google Chrome extension to try out early Roblox site features before they're officially out without any programming experience.

Nov 28, 2022

⚒ A JavaScript wrapper for the ROBLOX OpenCloud API.

⚒ A JavaScript wrapper for the ROBLOX OpenCloud API.

A JavaScript wrapper for the ROBLOX OpenCloud API. Introduction Recently at RDC 2022, ROBLOX announced OpenCloud API endpoints for DataStore, Messagin

Nov 11, 2022

A futuristic and lightweight data platform for upcoming ROBLOX games.

A futuristic and lightweight data platform for upcoming ROBLOX games.

A lightweight data platform for ROBLOX ⚠ Please restar this repo as it was accidentally deleted. We're still actively recovering. Introduction Datalin

Dec 15, 2022

Argon - extension for VS Code and plugin for Roblox allowing easy two-way sync of code and instances

Argon - extension for VS Code and plugin for Roblox allowing easy two-way sync of code and instances

About Argon is a simple two-way sync plugin for Roblox and extension for Visual Studio Code allowing developers not only to sync code but every possib

Dec 29, 2022

Simple "everyday CRUD" Postgres queries with perfect TypeScript types

Crudely Typed Simple "everyday CRUD" Postgres queries with perfect TypeScript types. Zero dependencies. Designed to work with pg-to-ts and node-postgr

Dec 26, 2022

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

🧩 TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

Dec 7, 2022
Owner
Australis
Roblox Development Studio that has been over-engineering for Roblox since 2014
Australis
Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.

expect-type Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time. Similar to

Misha Kaletsky 82 Jan 8, 2023
A TailwindCSS variant for class-based dark mode with CSS Modules.

A TailwindCSS variant for class-based dark mode with Svelte's scoped stylesheets and CSS modules. If you've ever tried to use TailwindCSS dark mode wi

Bryan Lee 13 Dec 1, 2022
HanAssist - Utilities to ease Chinese variant handling in user scripts and gadgets.

HanAssist 代码文档 HanAssist 是帮助中文维基百科及其他 MediaWiki 网站上的用户脚本和小工具更优雅地处理中文变体消息的实用程序。 本程序的目标是取代wgULS()和wgUVS()小工具。 HanAssist.localize( { hans: '一天一苹果,医生远离我。'

Diskdance 3 Oct 29, 2022
🌀 The gamma variant of Pastebin

snip ?? The gamma variant of Pastebin Supabase Hackathon · Demo ✨ Team Harsh Singh • Frontend (TypeScript) • GitHub • Twitter Ibrahim Hisham • Backend

Harsh Singh 51 Dec 28, 2022
They stole our free learn feature, so it's now time for an open source variant

Quizletbutfree This project was generated using Nx. ?? Smart, Fast and Extensible Build System Quick Start & Documentation Nx Documentation 10-minute

zerxal 2 Nov 13, 2022
solid material ui port (ported from blazor port)

solid-material-ui solid material ui port (porting from blazor port) In preparation for solid hack Turbo Mono-repository is used for component package

skclusive 18 Apr 30, 2022
SUID is all a set of utils and components ported from MUI Core and much more.

Solid.js User Interface Design (SUID) A port of Material-UI (MUI) built with Solid.js SUID is all a set of utils and components ported from MUI Core a

null 311 Jan 1, 2023
Activate The Open Web ™ ("Activate Windows" watermark ported to the web)

Activate-Web The "Activate Windows" watermark ported to Open Web ™. Inspired by activate-linux. As of 0.1, it’s a Web Component written in TypeScript,

Blair Noctis 8 Jun 6, 2022
Super Mario 64 ported to JavaScript and WebAssembly via Emscripten.

SM64-JS Super Mario 64 ported to JavaScript and WebAssembly via Emscripten. REPL Takedown I was forced to take down the REPL for this project due to c

Sevenworks 12 Dec 23, 2022
A Roblox OpenCloud API wrapper for Deno (and NodeJS) written in TypeScript.

Active Development This module is currently in active development. Nothing is going to stay consistent as it reaches a stable release. Dynablox Docs |

null 12 Oct 28, 2022