Skip to content

--fix for "An explicit null check is required" breaks code #226

@verhovsky

Description

@verhovsky

What version of this package are you using?

What operating system, Node.js, and npm version?
macos 12.3
Node: v16.14.2
npm: 8.5.3

What happened?

write this code:

function stuff (arg: string, optionalArg?: string[]) {
  optionalArg = optionalArg || ['some', 'default', 'value']
}

run ts-standard --fix, it turns into

function stuff(arg: string, optionalArg?: string[]) {
  optionalArg = (optionalArg != null) || ['some', 'default', 'value']
}

which is broken because optionalArg = (optionalArg != null) sets optionalArg to true instead of keeping it as-is when it has a truthy value. Also, typescript doesn't even compile the new code because the variable can now be a string[] or boolean.

What did you expect to happen?

Code doesn't get changed or code gets changed to use the ?? operator instead.

Are you willing to submit a pull request to fix this bug?

no

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions