-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ts] 'private' modifier cannot appear on a type member. #25163
Comments
You can only use |
private means it should only be visible to members declared on the same type--but since interface members don't have implementations, there's no way to reference the private property. So it's just not allowed. |
It looks like this is a question rather than a bug report. This issue tracker is for tracking bugs and active work on TypeScript itself, rather than a general forum for programmers using TypeScript to get help or ask questions. You can ask questions on sites like Stack Overflow. We are not able to provide one-on-one support on the issue tracker. Next time, please read the issue template carefully - it has important information on what kinds of reports can be acted on here, as well as links to useful TypeScript resources. Thanks! |
I think there is a bit of confusion that leads up to this error. If the members defined in the class are private, and these same members are used in the interface, TS will not compile and complain that these members are not set as private.
Using the above will yield the following error:
So if setting access modifiers on interfaces is not possible why does TS give this sort of error which implies there are? I'm guessing the overall idea is to not add private members in an interface, and if that's the case, maybe TS could offer an error to imply something along those lines instead? |
do we currently have any way to mark interface properties as protected or private? Currently this is the best I can do: interface TestInterface {
prop1: number
prop2?: string
}
class TestClass {
protected prop1: TestInterface['prop1']
protected prop2: TestInterface['prop2']
} though this isnt a great solution because if |
I agree with @3desprit edit Apparently,
|
What about I'm using types for mixin classes (it is inevitable to have to step around the |
For example, it is necessary to use interfaces/types (and not type PossibleCustomElement<T extends HTMLElement> = T & {
protected connectedCallback?(): void // contrived example
disconnectedCallback?(): void
adoptedCallback?(): void
attributeChangedCallback?(name: string, oldVal: string | null, newVal: string | null): void
}
// This is a mixin only for Web Components! :)
export function WithUpdateMixin<T extends Constructor<HTMLElement>>(Base: T) {
class WithUpdate extends Base as unknown as Constructor<PossibleCustomElement<HTMLElement>> {
protected connectedCallback() {
if (super.connectedCallback) super.connectedCallback()
}
}
} And so you see, I want to be able to define certain possibly-existing method/properties from the For sake of argument, let's imagine that |
Here's what we can do to make a type with access modifiers, although it emits an unused runtime class (based on some actual code I have, much omitted): type Constructor<T = object, A extends any[] = any[]> = new (...a: A) => T
function Constructor<T = object>(Ctor: Constructor<any>) {
return (Ctor as unknown) as Constructor<T>
}
// using a `class` instead of `interface` or `type`!
class PossiblyWebComponent {
protected childConnectedCallback?(child: Element): void
protected childDisconnectedCallback?(child: Element): void
protected _isPossiblyDistributedToShadowRoot?: boolean
protected _distributedParent?: TreeNode
}
function AwesomeMixin<T extends Constructor>(Base: T) {
const Parent = SomeOtherMixin(Constructor<PossiblyWebComponent>(Base))
class Awesome extends Parent {
protected childConnectedCallback(child: Element) {
if (super.connectedCallback) super.connectedCallback(child)
}
// ...
}
return Awesome as Constructor<Awesome & InstanceType<T>>
} It is possible to run the code through some Babel or Webpack plugin to snip unused code. EDIT: Or maybe just use |
I think the solution lies in 'getters', here is an example: interface IFighter {
strength: number;
dexterity: number;
}
class Character implements IFighter {
protected _strength: number;
protected _dexterity: number;
...
get strength() { return this._strength }
get dexterity() { return this._dexterity }
} That way you can implement an interface and still keep class attributes protected. |
@Leandro-Hespanhol Thank you! |
Type script seems more and more time consuming and convoluted as I learn it on an ever deepening level |
I declared an interface with private property method as the following snippet:
Butt i got error with the context
[ts] 'private' modifier cannot appear on a type member.
in vscode.What's wrong?
The text was updated successfully, but these errors were encountered: