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
(To my knowledge) it is not possible to reference the type of a hashed private field, the same way you might reference a privately declared field.
Either enabling this syntax, or providing some intrinstic wrapper to access the type would be helpful in DRYing up code using hashed private fields.
Suggested syntaxes:
Foo.#attrName
PrivateHash<Foo, 'attrName'>
To be clear, my request is only to access the type of hashed private fields. The actual output JS and handling of hashed private fields should remain unchanged. Any newly supported syntax should be stripped out at transpile time.
π Motivating Example
Consider the following TS declaration of a class using two, differently declared private fields
This is currently valid typescript and is fully supported.
In some cases, it may be that "names" aren't only strings. Perhaps they can be complicated types such as string | null | () => string | NameClass | .... In such cases, it is convenient to write that union once, and reference it.
For typescripts private declarations, that is supported, but not for JS's hashed private fields.
classPerson{private_firstName: string|null|undefined|NameClass|some|complicated|union|of|types;
#lastName: string|null|undefined|NameClass|some|other|and|different|union|of|types;constructor(firstName: Person['_firstName'],// FAILS WITH TYPESCRIPTlastName: Person.#lastName,// OR PrivateHash<Person, 'lastName'> OR Person['#lastName']){this._firstName=firstName;this.#lastName =lastName;}}
π» Use Cases
This feature is primarily one of convenience, attempting to achieve "feature parity" with Typescripts private fields.
If the goal is to only write the type once (DRY) then the best workaround, for now, is to declare a type/interface and reference that in both the attribute declaration and usages. Arguably that makes the attribute itself not the "source of truth" for it's own type.
# prefixed fields are actually a JS feature, and therefore usage is restricted in ways that typescript cannot control. In the example above, it is possible to reference the type outside the class as well:
constperson=newPerson('Joe','Schmoe');// Legal in TS + JStypeFirstName=typeofperson['_firstName'];constfirstName=person['_firstName'];// Illegal in TS + JStypeLastName=typeofperson.#lastName;constlastName=person.#lastName;
In this case, I would be fine with TS disabling access (i.e. do not apply this feature request) to hashed private fields outside of the class itself, to mirror actual usage.
The text was updated successfully, but these errors were encountered:
Worth noting the _ prefix is just a naming pattern to help indicate private. It is irrelevant to the actual functionality of typescript private vs hashed private fields
Suggestion
π Search Terms
hash private type reference # namespace exist
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
(To my knowledge) it is not possible to reference the type of a hashed private field, the same way you might reference a privately declared field.
Either enabling this syntax, or providing some intrinstic wrapper to access the type would be helpful in DRYing up code using hashed private fields.
Suggested syntaxes:
To be clear, my request is only to access the type of hashed private fields. The actual output JS and handling of hashed private fields should remain unchanged. Any newly supported syntax should be stripped out at transpile time.
π Motivating Example
Consider the following TS declaration of a class using two, differently declared private fields
This is currently valid typescript and is fully supported.
In some cases, it may be that "names" aren't only strings. Perhaps they can be complicated types such as
string | null | () => string | NameClass | ...
. In such cases, it is convenient to write that union once, and reference it.For typescripts
private
declarations, that is supported, but not for JS's hashed private fields.π» Use Cases
This feature is primarily one of convenience, attempting to achieve "feature parity" with Typescripts
private
fields.If the goal is to only write the type once (DRY) then the best workaround, for now, is to declare a type/interface and reference that in both the attribute declaration and usages. Arguably that makes the attribute itself not the "source of truth" for it's own type.
#
prefixed fields are actually a JS feature, and therefore usage is restricted in ways that typescript cannot control. In the example above, it is possible to reference the type outside the class as well:In this case, I would be fine with TS disabling access (i.e. do not apply this feature request) to hashed private fields outside of the class itself, to mirror actual usage.
The text was updated successfully, but these errors were encountered: