-
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
Ability to get setter types (because now they can be different than getter types). #43729
Comments
Or, in other words, based on structural typing, Maybe the assignability test needs to look at setter types under the hood? This would make it work without adding a syntax addition. It seems that really we only care about the getter type when we're getting the value. So the assignability check would need to use setter types of the destination object, or getter types of the source object. F.e. in |
To add my use case, I would like consumers of my class to be able to assign a setter with a value T or a function that returns a value T, and the on the getter return T (evaluate the function as needed). The motivation is to:
class Foo {
constructor(spec?: Partial<Foo>) {
Object.assign(this, spec);
}
_bar : string | (() => string) = ''
public get bar(): string {
if (typeof this._bar === 'function') {
return this._bar();
}
return this._bar;
}
public set bar(value: string | (() => string)) {
this._bar = value;
}
}
//Using assignment with string is fine
let foo = new Foo();
foo.bar = "Hello";
console.log(`string assigned setter evaluates to${foo.bar}`)
//Using assignment with function is fine
foo.bar = () => "Hello";
console.log(`function assigned setter evaluates as ${foo.bar}`)
//Using Partial<T> with value as string is fine
let foo2 = new Foo({bar: "Hello"});
console.log(`partial assignment of setter to string results in ${foo2.bar}`)
//Using Partial<T> with () => string gives a type error
let foo3 = new Foo({bar: () => "Hello"} ); //compiler complains unless I force type with 'as any'.
console.log(`Partial assignment if setter with function results in ${foo3.bar}`) |
Another use case: Based on this component class type, I wish to define the typings for my library for React JSX, Preact JSX and other JSX libraries. However, when I use TypeScript's mapped type syntax, or |
Closing as duplicate of |
⭐ Suggestion
Now that TS 4.3 introduces the awesome ability to have setter types be a different type than getters, it would be useful to be able to get the type of a setter or getter (not just the property as a whole, which defaults to the type of the getter).
🔍 Search Terms
typescript 4.3 get type of setter
✅ Viability Checklist
My suggestion meets these guidelines:
📃 Motivating Example
Basically this example shows the issue:
playground
💻 Use Cases
It is currently possible to achieve the desired effect by manually setting the types of the
props
object, but this is more cumbersome, especially when there are many types. I.e., the burden is on the end user instead of the class author (class author can serve many, instead of end users all having to write type defs).Here's an example of that. The end user of the class
as any as number
, which is not ideal.Ideally the class author would be able to write the
set
method with setter types, something like:I don't know what the syntax would be, but the idea is that the result of
setters this
is a type that has the types of the setters for properties that are originally accessors. The other properties works the same as usual.Or maybe we need types like
get this['foo']
andset this['foo']
that can return the getter or setter type of a specific property, and then one can create a mapped type that can use theget
orset
type operators.The text was updated successfully, but these errors were encountered: