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
Currently, TypeScript requires that the get and the set types of properties match. However, there are scenarios where, I think, it would be very useful to have a set that is more accepting than the get.
For example: imagine you have a property of Array<Array<string>> type. Today, this means that both the get and set would have to be of the same type, and so setting the property would always be done via the array notation.
It seems reasonable, however, to allow the "setter" to be more forgiving and let it coalesce a single string value into an array automatically. That way, when setting the property, you could either do cell.values = [[1]] or just cell.values = 1, and the class would accept either syntax. But for return values, it would always return an array type, and so the get of the values property should still be marked as Array<Array<string>> (that way you would get all the good IntelliSense of having .filter, .forEach, etc., on the return type).
Today, this code would trigger an error that "get and set accessors must have the same type".
The only workarounds available today are both less than ideal:
Set the property of values -- both "set" and "get" -- to any. But in doing so, you lose all IntelliSense.
Mark the property -- both "set" and "get" -- as Array<Array<string>>|string. However, while this solves the "set" problem, the "get" property loses a lot of the IntelliSense (.filter, .forEach), and is actually no longer true to reality (the return value is always an array, not an "array or a string")
Would be great if the scenario of a "more inclusive set property" could be supported.
The text was updated successfully, but these errors were encountered:
Currently, TypeScript requires that the
get
and theset
types of properties match. However, there are scenarios where, I think, it would be very useful to have aset
that is more accepting than the get.For example: imagine you have a property of
Array<Array<string>>
type. Today, this means that both the get and set would have to be of the same type, and so setting the property would always be done via the array notation.It seems reasonable, however, to allow the "setter" to be more forgiving and let it coalesce a single string value into an array automatically. That way, when setting the property, you could either do
cell.values = [[1]]
or justcell.values = 1
, and the class would accept either syntax. But for return values, it would always return an array type, and so theget
of the values property should still be marked asArray<Array<string>>
(that way you would get all the good IntelliSense of having.filter
,.forEach
, etc., on the return type).Today, this code would trigger an error that "get and set accessors must have the same type".
The only workarounds available today are both less than ideal:
Set the property of values -- both "set" and "get" -- to
any
. But in doing so, you lose all IntelliSense.Mark the property -- both "set" and "get" -- as
Array<Array<string>>|string
. However, while this solves the "set" problem, the "get" property loses a lot of the IntelliSense (.filter
,.forEach
), and is actually no longer true to reality (the return value is always an array, not an "array or a string")Would be great if the scenario of a "more inclusive
set
property" could be supported.The text was updated successfully, but these errors were encountered: