-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Applying a constrained generic with type parameters as the argument type doesn't properly catch constrained fields #52309
Comments
You should explicitly provide the constraint as a context to help infer the correct type of const DefineObject = <
Props extends PropertiesType,
RequiredProps extends RequiredPropertiesType<Props>,
Def extends JSONSchemaObjectType<Props, RequiredProps>
- >(definition: Def) => {
+ >(definition: Def & JSONSchemaObjectType<Props, RequiredProps>) => {
}
|
@whzx5byb this constrains the argument but causes any conditional typing that relies on |
Can you go into more detail? The above solution is effectively correct; there's not a way to exclude candidates from inference like you would need in order to make this to work. |
Treating as duplicate of #14829 |
Sorry for the silence here, I've been looking into the resulting conditional typing failure and haven't been able to reproduce what we're seeing in a simplified TS Playground. Applying the recommended solution appropriately applied the constraint on the I've since realized that the test failures we're seeing are resolved if we assign the result of // This causes test failures in our inferred runtime typing
DefineFunction({
input_parameters: {
properties: {
myObject: DefineObject({
type: Schema.types.object,
properties: { req: { type: "string" }, opt: { type: "string" } },
required: ["req"],
});
},
required: ["myObject"],
},
});
// This does not
const MyObject = DefineObject({
type: Schema.types.object,
properties: { req: { type: "string" }, opt: { type: "string" } },
required: ["req"],
});
DefineFunction({
input_parameters: {
properties: {
myObject: MyObject
},
required: ["anObject"],
},
}); I'm still working on providing a Playground example, but please let me know if this behavior is familiar to you, if I should close this issue and open a new issue when I have more details, or if a pointer to where this creates problems in our actual code base would be helpful 🙇 |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
generic constraints, generic types, multiple generics, generic type variable, type parameters
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The
required
array accepts values that are not keys used in it's siblingproperties
🙂 Expected behavior
The
required
array is constrained to the keys used in it's siblingproperties
The text was updated successfully, but these errors were encountered: