-
Notifications
You must be signed in to change notification settings - Fork 245
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
Add support for Omit
and Partial
types to aid in extending Props interfaces
#4468
Comments
Thanks for the feature request.
This is correct, jsii could do this. However the issue is more complicated for jsii languages that use a stronger type system. What does it mean in Java if I'm not convinced the added ambiguity is worth the added convenience. In any case, today you can use third party tools like mrgrain/jsii-struct-builder to achieve a similar result. |
This is not the desired behavior. Omit and Partial would create new types that don't extend the types they are created from. export interface MyServiceProps extends Omit<rds.ServerlessClusterProps, "prop"> {} It is common in Constructs to reduce or simplify the interface of the Constructs it wraps. Being able to create those narrow types without duplication is the feature request. |
Acknowledged. That isn't your desired behavior. I'm posing the question if it would be confusing for some users. I think
Sure, I'm very familiar with the problem. "without duplication" is the challenge here. I believe it might cause ambiguity and am wondering about your view on how to resolve the ambiguity or if it even is a problem. |
I'm not sure I understand the ambiguity you're referring to.
Why not? It is identical. It copies the properties across including their references and documentation.
What is the ambiguity? I am proposing that
I am not following. What problem?
Yeah, that is what should happen in Java and other languages - a new type should be generated with properties copied across. That's ok because they are generated from TypeScript. The scope of this feature request is for the TypeScript->JSII compiler. The desired behavior is to create new types that behave as if they were written by hand. They do not extend the types they are derived from. |
To start off, I would really welcome support for this in JSII, as indeed it makes it a lot easier to implement "L3" construct libraries. (For reference, I am trying to use JSII to provide a Python version of an internal Typescript L3 construct library, ran into issues with interfaces extending Omit<...> that caused JSII to fail, found a workaround that at least made JSII succeed in producing a Python version, but then found out when trying to use it that JSII had basically ignored the "omitted interfaces", so the Python version is not usable. Then I googled and found this issue). Regarding ambiguity, as Sam mentioned, a new interface (e.g., However, I do see a possible issue when the existing interface changes (which happens often with new minor CDK versions, as new features are implemented). Consider an Now, CDK releases a new minor version with a feature update on the interface, and it has changed to
(By the way: I had a quick look at jsii-struct-builder by @mrgrain, but I'm not sure it's really a solution. The main drawback that I see is that basically you have to start maintaining interface types outside of the locations where they logically belong, and also that (compared to the simple |
My concern about ambiguity originates entirely from the fact that TypeScript uses the
Yes, this is also a concern. I think the feature could go two ways. For non-TS languages, inference will have to happen at compile time and probably use whatever version of |
Describe the feature
I'd like to be able to use
Omit
andPartial
to create a sub-set of a Construct class I am sub-classing.Use Case
I regularly extend L2 Constructs with opinionated (and more importantly, narrower) interfaces. For example, a L3 may require the underlying DB be Postgres and doesn't allow the
engine
to be set. But, I still want to expose the rest ofrds.ServerlessClusterProps
as optional props to be configured. Doing this today requires redundant copy/pasting of the props interfaces into the L3 Constructs which is wasteful and annoying in cases where the interfaces have deep hierarchies.This should be possible:
Proposed Solution
JSII can leverage the type checker to resolve the properties on the omitted type and emit a new class that is a subset.
Other Information
No response
Acknowledgements
CDK version used
2.126.0
Environment details (OS name and version, etc.)
MacOS 14.1
The text was updated successfully, but these errors were encountered: