coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement 💪 with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
698 stars 40 forks source link

[typescript/convert-generic-to-shorthand]: Creates TypeErrors for intersections with no overlap. #173

Closed ElPrudi closed 1 year ago

ElPrudi commented 1 year ago

Normally, this rule fixes this

const stuff: Array<number | boolean> = []

to this:

const stuff: number[] | boolean[]

Sadly, this results into a TypeError, since number and boolean have no overlap:

export default class Test {
    stuff: Array<number | boolean>

    add(x: number | boolean): void {
        this.stuff.push(x) // No error, because the element can be either a number or a boolean inside this array.
    }
}
export default class Test {
    stuff: number[] | boolean[]

    add(x: number | boolean): void {
        this.stuff.push(x) // TypeError! You can not add a boolean to a number array or a number to a boolean array!
    }
}
coderaiser commented 1 year ago

Why do you need number and boolean on the same array?

How do you suggest to fix it, to:

const stuff: (number | boolean)[];

Or just skip?

coderaiser commented 1 year ago

Just fixed 🎉 ! Is it works for you?

ElPrudi commented 1 year ago

This is often needed when you need to transform data that was collected in an array. Most of the time, these do have some kind of type overlap, but sometimes they don't.

But yes, it works now. Thank you very much :)