Skip to content

add type-guard for void #1806

@fdecampredon

Description

@fdecampredon

There is a type that is quite interesting in typescript void | {}, this type has some unique behavior, everything can be assigned to this type, but the compiler does not infer any property for this type unlike for {} that inherits the basic object properties, the difference with {} is subtile but still pretty interesting:

function func1(val: {} ) {
    val.hasOwnProperty('hello'); // no error
}
function func2(val: {} | void) {
    val.hasOwnProperty('hello');  // error:  Property 'hasOwnProperty' does not exist on type 'void | {}'.
}

However there is not typeguard for void, it could be interesting to add some type guards for this case:

  • typeof val !== 'undefined'
  • val !== undefined
  • val != undefined
  • val != null

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions