-
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
Infer class type from keyof class type #37384
Comments
Stack Overflow might be a better place for this, since this does seem to be working as intended. I'd say that It does not scan through all other types mentioned in the code to see if there's another type which it can use instead. That might make sense in a nominal type system where you could hope to enumerate all types, but in a structural one where types can exist without explicit declarations, it's a non-starter. Hope that makes sense; good luck! |
@jcalz thank you! Before you close the issue, I hope it's okay if I run a single use case by you, as I believe this nominal-ish feature is one of importance to the language. Let's say you're using a validation library, which exports a decorator factory import {Validate, Validator} from "some-wild-validator"
class User {
name: string;
@Validate(validator)
age: number;
}
const validator: Validator<User, "age"> = (user, age) => {
// do something
} Within the above If it were possible to use the infer type to work backwards from const validator: Validator<User["age"]> = (user, age) => {
// do something
} 2 generic arguments -> 1 generic argument. Not a huge difference, but ice cool nonetheless. I'd love to hear thoughts on this. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
@harrysolovay Have you found any solution to infer the rest part of the interface? I ran into this issue this week and still have not figuredout how to make it. |
TypeScript Version: 3.8.2
Search Terms: infer, class, type, from, keyof, class, member, prop, deduce
View on TS Playground
Description:
In this example, one might expect for
MyClassInferred
to have a signature ofMyClass
. Instead it has the following signature:{ a: any } | { b: any }
.Is this by design?
Thanks!
Kind regards,
Harry
The text was updated successfully, but these errors were encountered: