Skip to content
📖 Auto vs TypeScriptBook contents

Narrowing ​

TypeScript uses narrowing to refine a value's type within conditional branches. Auto uses is expressions for pattern matching, which provides a more structured approach to the same problem.

Pattern Matching with is ​

Auto's is expression inspects a value and matches it against patterns. When a pattern matches, the corresponding branch executes. This is Auto's primary mechanism for narrowing — it replaces TypeScript's typeof checks, instanceof checks, and discriminated union switches with a single consistent syntax.

Pattern matching with is

The is expression transpiles to a TypeScript switch statement. Each pattern binds variables (like n and t) that are available within the branch body. In TypeScript, the equivalent would use a switch on the _tag field of a discriminated union.

Truthiness Narrowing ​

Auto treats 0, empty strings, null, and false as falsy — the same as TypeScript. You can use any value directly in an if condition:

Truthiness narrowing

This works identically in both Auto and TypeScript. The if count condition narrows the type implicitly — inside the if branch, the compiler knows count is truthy.

Discriminated Unions ​

TypeScript's discriminated unions use a common property (like kind) to narrow types. Auto uses enums with tagged variants, which transpile to discriminated unions automatically:

Discriminated unions with enum

Each enum variant becomes a { _tag: "...", value: T } object. The is expression transpiles to a switch on the _tag field, which is exactly how TypeScript handles discriminated unions.

Exhaustiveness ​

Auto's is expression requires you to handle every variant. If you add a new variant to an enum but forget to handle it in an is expression, the compiler will warn you. TypeScript achieves the same effect with never type checking in switch default cases.

TypeScript-Only Narrowing Features ​

The following TypeScript narrowing features do not have direct equivalents in Auto, because Auto uses is pattern matching as its unified narrowing mechanism.

typeof Guards ​

typescript
// TypeScript only
function format(value: string | number) {
    if (typeof value === "string") {
        return value.toUpperCase();
    }
    return value.toFixed(2);
}

In Auto, use is pattern matching with enum variants instead.

instanceof Narrowing ​

typescript
// TypeScript only
function formatDate(date: Date | string) {
    if (date instanceof Date) {
        return date.toISOString();
    }
    return date;
}

Auto does not have classes with runtime type information in the same way. Use enum-based tagging instead.

Type Predicates ​

typescript
// TypeScript only
function isFish(pet: Fish | Bird): pet is Fish {
    return (pet as Fish).swim !== undefined;
}

Auto's is expression handles this implicitly — each branch already has the narrowed type.

Quick Reference ​

TypeScriptAutoDescription
typeof x === "string"x is str => ...Type check
x instanceof DateEnum taggingRuntime type check
switch (x.kind)x is { ... }Discriminated union
if (value)if valueTruthiness
x is Type (predicate)is branch bindingType predicate

Released under the MIT License.