question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression in 4.8 where string union type widens to string

See original GitHub issue

Bug Report

🔎 Search Terms

string union widening, string union regression, union widening

🕗 Version & Regression Information

This regression occurred in 4.8.0, and can be reproduced with the 4.8.0-beta, and nightly (4.9.0-dev.20220904) tag in TS Playground. This works with 4.7.4.

  • This changed between versions 4.7.4 and 4.8.0-beta

I feel like this might be related to the Improved inference from binding patterns change

⏯ Playground Link

Playground link with relevant code

To reproduce, hover over the “isWorking” variable to see that the type definition includes a union of the strings. Switch to 4.8.0-beta, and note that the type definition is now just string.

💻 Code

interface Guard<T> {
    (val: unknown): val is T;
}

type ObjectGuard<T> = {
    [key in keyof T]: Guard<T[key]>;
};

function isObject(val: unknown): val is Record<string, unknown> {
    return val !== undefined && val !== null && typeof val === 'object' && !Array.isArray(val);
}

function isObjectOfShape<T>(
    value: unknown,
    shape: ObjectGuard<T>,
): value is T {
    if (!isObject(value)) {
        return false;
    }
    let validShape = true;

    for (const key in shape) {
        const guard = shape[key];
        if (guard && !guard(value[key])) {
            return false;
        }
    }

    return validShape;
}

function createObjectGuard<T>(guard: ObjectGuard<T>) {
    return (val: unknown): val is T => isObjectOfShape(val, guard);
}

function asLiteral<T extends (string | boolean | number)[]>(...literals: T) {
    return (val: unknown): val is T[number] => {
        return literals.includes(val as T[number]);
    };
}

// See type of `isWorking` - should include the type key as a union of strings
const isWorking = createObjectGuard({
//    ^?
    type: asLiteral('these', 'should', 'be', 'a', 'union'),
});

🙁 Actual behavior

String union type is widened to string.

🙂 Expected behavior

String union type remains a union, rather than widening

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
HolgerJeromincommented, Sep 28, 2022

I think the milestone should be adjusted because that ship has sailed… 😃

1reaction
andrewbranchcommented, Sep 28, 2022

Thanks. I forgot to update this issue, but we decided that the fix looked a little risky to ship in a patch when weighed against the small number of reports of this issue we’ve gotten.

Read more comments on GitHub >

github_iconTop Results From Across the Web

why is TypeScript converting string literal union type to string ...
The type inferred for a property in an object literal is the widened literal type of the expression unless the property has a...
Read more >
Handbook - Unions and Intersection Types - TypeScript
A union type describes a value that can be one of several types. We use the vertical bar ( | ) to separate...
Read more >
input — Enter data from keyboard - Stata
You can input string variables by using input, but you must remember to indicate explicitly that the variables are strings by specifying the...
Read more >
Type Hinting Unions in doc strings comes up as "_SpecialForm"
If you're doing the type hinting inside of a doc string, with or without params, PyCharm complains that all instances of its use...
Read more >
Union type widening - Language Design - Scala Contributors
Currently, the compiler widens union types to a common supertype when ... def foo(x: String | Int) = println(x) val x = if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found