You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
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.
Activity
mhegazy commentedon Jul 16, 2018
you can achieve a similar pattern by slightly reordering your code to use immutable objects instead of mutations, e.g.
you can also achieve that using type assertions:
That siad, i do not think we have any plans to model side effects in the type system in the time being.
typescript-bot commentedon Jul 27, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
oldrich-s commentedon Sep 4, 2018
Mohamed Hegazy (@mhegazy) : I have run into the same problem again, this time with
MochaandChai:Chaithrows if thevaluedoes not exist, therefore after the assert function is called, the value should be of typenumber.For now, I have to manually retype values after each
assertcall, which is really not optimal.What solution do you propose to this problem?
RyanCavanaugh commentedon Sep 4, 2018
See #10421 and its linked issues
oldrich-s commentedon Sep 5, 2018
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
Which is probably not optimal either.
Basically I would like to have type guards without the
ifstatement requirement.RyanCavanaugh commentedon Sep 5, 2018
See also #8655