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
Use of a computed property name in a getter or setter leads to unexpected error A computed property name must be of type 'string', 'number', 'symbol', or 'any' even when the computed type does satisfy these types.
If this should be considered a feature report, (e.g. this is a known gap in the language, but just a misleading error) one motivating use case for improving the support of get and set could be achieving future strictness preventing readonly assigned to mutable.
In the short term (before readonly strictness), being able to use get with mapped types and computed property names and value types would mean that there are workarounds for cases when you want to PREVENT the passing of writeable objects and maybe warn that the object should have been readonly, as demonstrated in the imaginary Frozen<T> implementation in the code sample.
🔎 Search Terms
getter setter "computed property name"
🕗 Version & Regression Information
I don't know of a version this works with. Everything up to the Nightly at the time of writing experiences this bug.
Hovering over the error in the get or set definitions (which are identical to the working property definition above) leads to these errors...
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.
Cannot find name 'k'
'K' only refers to a type, but is being used as a value here.
🙂 Expected behavior
I would expect it to be possible to define the presence and absence of getters and setters using computed property names.
The text was updated successfully, but these errors were encountered:
This is all invalid syntax, which is leading to some red herrings in the error messages as the parser does its best to recover into a valid state. Computed property declarations are not the same thing as mapped types, though they both use square brackets to describe the type of a key in a resulting object type:
// mapped typestypeT={[KinSomeUnion]: any}// Mapped types are not interfaces or object literal types!// `[K in SomeUnion]: any` is not a property!typeU={[KinSomeUnion]: any;foo: any;// <-- Syntax error!}// Mapped types support adding a `readonly` modifier to resulting keys:typeV={readonly[KinSomeUnion]: any}typeW={+readonly[KinSomeUnion]: any}// Or removing `readonly` (only makes sense on homomorphic mapped types):typeX={-readonly[KinkeyofT]: T[K]}// computed propertiestypeY={[someValue]: string;// <-- real propertyfoo: number;}// they can equivalently go on interfacesinterfaceI{[someValue]: string;foo: number}// interface and object literal property declarations support// `get` and `set` modifiers:interfaceJ{get[someValue]: string;}
Syntax for what you’re looking for doesn’t exist because the semantics you want don’t exist. I think this is essentially a duplicate of #21759, and #13347 is very relevant as you’ve already found.
Bug Report
Use of a computed property name in a getter or setter leads to unexpected error
A computed property name must be of type 'string', 'number', 'symbol', or 'any'
even when the computed type does satisfy these types.If this should be considered a feature report, (e.g. this is a known gap in the language, but just a misleading error) one motivating use case for improving the support of get and set could be achieving future strictness preventing readonly assigned to mutable.
In the short term (before readonly strictness), being able to use get with mapped types and computed property names and value types would mean that there are workarounds for cases when you want to PREVENT the passing of
writeable
objects and maybe warn that the object should have been readonly, as demonstrated in the imaginaryFrozen<T>
implementation in the code sample.🔎 Search Terms
getter setter "computed property name"
🕗 Version & Regression Information
I don't know of a version this works with. Everything up to the Nightly at the time of writing experiences this bug.
⏯ Playground Link
Playground demonstrating error
💻 Code
🙁 Actual behavior
Hovering over the error in the get or set definitions (which are identical to the working property definition above) leads to these errors...
🙂 Expected behavior
I would expect it to be possible to define the presence and absence of getters and setters using computed property names.
The text was updated successfully, but these errors were encountered: