-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[Feature Request] fieldof similar to keyof but include private and protected field #46802
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
Comments
The only cases I could imagine this being potentially useful are to allow As for your case you declare I'm pretty sure you just want the argument to be class Childrable {
public entity!: A;
public readonly children: A[] = [];
public addChild(...children: A[]) {
// ...
const child = children[0];
if (child) {
// use type assertion here to indicate we are breaking the declared type in A
(child as Writable<A>).parent = this.entity;
this.children.push(child);
}
return this;
}
} |
I found a way by abstract class with a method export abstract class Token extends Entity {
public readonly parent?: Token;
isContainer(): this is Container {
return false;
}
isPrimitive(): this is Primitive {
return false;
}
useWritable(): Writable<this> {
return this;
}
}
export class Container extends Token {
declare public readonly parent?: Container;
public readonly children: Token[] = [];
public override isContainer(){
return true;
}
protected override get Renderer() {
return Renderer;
}
public addChild( ...children: Token[] ) {
if ( children.length > 1 ) {
for ( const child of children ) this.addChild( child );
} else {
const child = children[0];
if ( child ) {
if ( child.parent?.isContainer() ) child.parent.removeChild( child );
child.useWritable().parent = this;
this.children.push( child );
}
}
return this;
}
} |
I have a use case for this wherein I'm using branded types to create a branded class (similar to what https://www.npmjs.com/package/@prosopo/ts-brand does) and the class that I am branding contains a private field.
So the instance I create is actually a
EDIT: So as not to waste your time, I want you to know that I'm not currently blocked on this. I ended up removing that I don't really see the point in handling |
Suggestion
π Search Terms
fieldof , mapped type, mapped private protected
Can maybe related: #35416 #4822
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
add a new utility
fieldof
for map easily all keys field include, private and protectedπ Motivating Example
i have similar issue here where some pattern make complications !
playground
It would be great to see a easy way to handle more patterns with
protected
,private
fields when we map types for specific case.In my upper example,
Childrable
is a component where you can attach to EntityA
, andChildrable
have some method where allowed to write in somereadOnly field
from A.But map to readOnly will remove private and protected field and will create issue, in this case
fieldof
will fix the issue, becaue we want allow Writable, in context of component have method for mutate entities.π» Use Cases
So same as
keyof
, but include allprivate
andprotected
fields for specific pattern.This will also maybe unlock more powerful features and patterns, but let just talk about this specific case for now.
The text was updated successfully, but these errors were encountered: