Open jonlepage opened 2 years ago
The only cases I could imagine this being potentially useful are to allow Required<X>
and Writable<X>
to be assignable to X
for any class X
with protected fields. In any other case this would break the most basic use case of protected methods since protected fields are checked for collision between classes not assignability.
As for your case you declare A.parent
to be readonly
which means it shouldn't be assigned outside the constructor and you are asking for a feature to literally allow you to break that assurance, so I'm not convinced even for the Writable
case it is a good idea.
I'm pretty sure you just want the argument to be A
not Writable<A>
so that it is expected for external users to pass actual instances and then use a type assert inside the method to allow you to modify the property despite that not being allowed according to the declaration of A
. playground
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 useWritable()
But it would still be interesting to be able to extract all the fields.
Am not fan of type assertion expression for readability.
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;
}
}
Suggestion
š Search Terms
fieldof , mapped type, mapped private protected Can maybe related: https://github.com/microsoft/TypeScript/issues/35416 https://github.com/microsoft/TypeScript/issues/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 casefieldof
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.