Skip to content

Mutable type guards #25679

Closed
Closed

Description

Search Terms

mutable type guards

Suggestion

Allow for mutable type guards (i.e. type guards that are always true).

Use Cases

I would like to extend HTMLElements without wrapping them. So basically I would add "extra" property to a HTMLElement that would extend its capabilities.

Examples

function setExtra<T>(el: T): el is T & { extra: { stuff: {} } } {
  (el as any).extra = { stuff: {} }
}

const el = document.createElement('div')
el.extra // type is HTMLDivElement, therefore fail

setExtra(el)

el.extra // type is HTMLDivElement & { extra: { stuff: {} } }, therefore OK

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Activity

  1. mhegazy commented on Jul 16, 2018

    @mhegazy
    Contributor

    you can achieve a similar pattern by slightly reordering your code to use immutable objects instead of mutations, e.g.

    const el = setExtra(document.createElement('div'));

    you can also achieve that using type assertions:

    const el = <HTMLDivElement & { extra: { stuff: {} }}> document.createElement('div')

    That siad, i do not think we have any plans to model side effects in the type system in the time being.

  2. typescript-bot commented on Jul 27, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  3. oldrich-s commented on Sep 4, 2018

    @oldrich-s
    Author

    Mohamed Hegazy (@mhegazy) : I have run into the same problem again, this time with Mocha and Chai:

    let value: number | undefined
    
    Chai.assert.exists(value)
    
    // value should be now `number` and not `number | undefined`
    

    Chai throws if the value does not exist, therefore after the assert function is called, the value should be of type number.

    For now, I have to manually retype values after each assert call, which is really not optimal.

    What solution do you propose to this problem?

  4. RyanCavanaugh commented on Sep 4, 2018

    @RyanCavanaugh
    Member

    See #10421 and its linked issues

  5. oldrich-s commented on Sep 5, 2018

    @oldrich-s
    Author

    Thank you.

    If I am not mistaken, the link you sent discusses block scope type assertions.

    What I would like to do is to mutate the type of variables that are sent into a function as function parameters from inside of the function. That would probably break the "block scope" rule discussed in the provided link, right? Then I would have to do

    let value: number | undefined
    
    Chai.assert.exists(value)
    
    assume value is number
    
    ...
    

    Which is probably not optimal either.

    Basically I would like to have type guards without the if statement requirement.

  6. RyanCavanaugh commented on Sep 5, 2018

    @RyanCavanaugh
    Member

    See also #8655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraints

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions