-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Could be instantiated with a different subtype of constraint #50027
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
I don't see a bug here.
function f() { }
f.prop = "hello"; Your f1(f).prop.toUpperCase(); // okay?! but of course there's a runtime error, since On the other hand, f2(f).prop.toUpperCase(); // compiler error
// ~~~~ <-- Property 'prop' does not exist on type 'A' So |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I think I'm running into a similar issue and could use some help. The goal is: accept a generic type of one of two options, but have one be a default. Return that type or I don't understand why T "could be instantiated with an arbitrary type" when I'm specifying that it could only be
Its working as I intend from the caller perspective. (The developer can call |
I wish this was working, it would be really cool! It extends the similar behavior that the `NBTData` data does, where it attaches the format options types as a generic on the `NBTData` type itself. This would be neat to define the shape of the `NBTData` object fully, including it's content, and it's format options. While writing that last part, I realized a better way to handle this, and may be generics on the `NBTData` object aren't the way to go. Since it's only the types that have to stay static, maybe it makes more sense to manage these from a level above the `NBTData` object. I would have liked to be able to constrain the values you pass into the `NBTData` object, by using generic constraints, but it seems like this prevents you from allowing the parameter to be optional. I think I found some options to describe the error, I think they are related to the issue I'm having. The type issues are only occurring when the options parameter is applied with a default value, or set to anything other than the type of itself. Even with type casting to either `FormatOptions` or `U`, it won't let you assign anything to the parameter. https://stackoverflow.com/questions/56505560/how-to-fix-ts2322-could-be-instantiated-with-a-different-subtype-of-constraint microsoft/TypeScript#50027 https://www.reddit.com/r/typescript/comments/rncy44/implementing_function_types_with_a_generic/ https://news.ycombinator.com/item?id=30247357 https://stackoverflow.com/questions/67397978/ts2322-generic-not-propagated-correctly microsoft/TypeScript#43820 After discussing that, I think that's probably the way to go. Maybe the `NBTData` object shouldn't be where things are dictated, hmm. It seems to work really nice with the `NBT.read()` and `NBT.write()` functions though, and I wish I didn't have to lose that functionality. The next stage I may try for this is to make a generic for each option parameter. I don't really like the idea of that though. Let's see what happens. This is so close! I realized the `const U ...` const declaration for the generic works great here too, which makes the `NBTData` properties reflect the options you pass into the options object, with exact values. So passing in `"Level"` for the name option will reflect `"Level"`, rather than only `string`, which is what happens without using `const U`.
Bug Report
π Search Terms
not assignable to the constraint of type
could be instantiated with a different subtype of constraint
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The following error is happening with
f1
but notf2
:π Expected behavior
Defining
f1
should work fine.The text was updated successfully, but these errors were encountered: