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
{{ message }}
Repository navigation
Expressions in JS special assignments don't use jsdoc for contextual type #25571
[ts] Type 'string' is not assignable to type '"error" | "done"'.
test.js(3, 4): The expected type comes from property 'status' which is declared here on type 'DoneStatus | ErrorStatus'
(property) status: string
Expected behavior:
JS behaves the same as TS version
changed the title [-]Can't assign immediate values to tagged union properties in JS mode[/-][+]Expressions in JS special assignments don't use jsdoc for contextual type[/+]on Jul 11, 2018
I'm going to make a partial fix that makes these assignments appropriately context sensitive, which fixes this bug, but then work on a permanent fix that makes the type of other assignments, like
correctly assign { status: 'done' } as the type of F.prototype. This a bigger change that is probably not appropriate for 3.0.1. Hard to say without trying it first.
Nathan Shively-Sanders (@sandersn) is this the best way of asserting member types? i.e. the one thing I wasn't sure of was should I be using some @property assertion instead? I just couldn't find a way to get it to work - if I add a @property tag to my class then TS interprets it as a static property, not instance.
On my reading of jsdoc docs it seems using @type at the assignment point is considered correct, just wanted to check :)
In Javascript, the pattern that the compiler expects is modelled on Closure:
classC{/** @param {number} x */constructor(x){this.x=x;/** @type {number | undefined} */this.y;}}
Basically it's just looking for assignments to this.propertyName. If the assignment has an initialiser, it will use its type unless there is a type annotation. The compiler actually looks in any method, not just constructors, but assignments in the constructor are definite -- they don't include undefined in the type like assignments in methods do.
method(){this.z='hi'// z: string | undefined}
We have a proposal for a @property tags that would go above the class, but those would result in slower code than just initialising properties in the constructor since the VM will end up creating fewer hidden classes for classes with uniform sets of properties.
Activity
mhegazy commentedon Jul 11, 2018
looks like the contextual type is not picked up correctly for the special property assignment case.
[-]Can't assign immediate values to tagged union properties in JS mode[/-][+]Expressions in JS special assignments don't use jsdoc for contextual type[/+]sandersn commentedon Jul 12, 2018
I'm going to make a partial fix that makes these assignments appropriately context sensitive, which fixes this bug, but then work on a permanent fix that makes the type of other assignments, like
correctly assign
{ status: 'done' }as the type ofF.prototype. This a bigger change that is probably not appropriate for 3.0.1. Hard to say without trying it first.Explicitly typed js assignments: context sensitive
Explicitly typed special assignments are context sensitive (#25619)
akdor1154 commentedon Jul 13, 2018
Nathan Shively-Sanders (@sandersn) is this the best way of asserting member types? i.e. the one thing I wasn't sure of was should I be using some
@propertyassertion instead? I just couldn't find a way to get it to work - if I add a@propertytag to myclassthen TS interprets it as a static property, not instance.On my reading of jsdoc docs it seems using
@typeat the assignment point is considered correct, just wanted to check :)sandersn commentedon Jul 16, 2018
In Javascript, the pattern that the compiler expects is modelled on Closure:
Basically it's just looking for assignments to
this.propertyName. If the assignment has an initialiser, it will use its type unless there is a type annotation. The compiler actually looks in any method, not just constructors, but assignments in the constructor are definite -- they don't include undefined in the type like assignments in methods do.We have a proposal for a
@propertytags that would go above the class, but those would result in slower code than just initialising properties in the constructor since the VM will end up creating fewer hidden classes for classes with uniform sets of properties.