You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: support class covariant overrides in kernel, tools and docs (#4925)
Updates docs and additional tools with support for class covariant
overrides.
---
By submitting this pull request, I confirm that my contribution is made
under the terms of the [Apache 2.0 license].
[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Copy file name to clipboardExpand all lines: gh-pages/content/user-guides/lib-author/typescript-restrictions.md
+29-16Lines changed: 29 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ coexist in the same closure, it is recommended to also declare all such dependen
18
18
19
19
Occasionally, a dependency on a *non-jsii module* is useful. Since such dependencies do not have generated bindings in
20
20
all the supported languages, they must be bundled with the *jsii module* that depends on them, by adding the library
21
-
into the `bundleDependencies` array in `package.json`.
21
+
into the `bundleDependencies` array in `package.json`.
22
22
23
23
The API of the *jsii module* can not expose any type from bundled dependencies, since those types would not be available in other languages.
24
24
TypeScript files that include a non-jsii dependency (e.g. a lambda handler for an AWS CDK Construct) cannot be exported from the `main`/`types` entry point.
@@ -137,40 +137,53 @@ export class Subclass extends Base {
137
137
### Covariant Overrides & Parameter List Changes
138
138
139
139
In **TypeScript**, overriding members can have a signature that differs from the overridden member as long as the new
140
-
signature is compatible with the parent. This is however not supported as:
140
+
signature is compatible with the parent. This is only partially supported in jsii as:
141
141
142
-
-**Java** and **C#** do not support omitting parameters when overriding or implementing a member
143
-
-**C#** does not support overriding or implementing a member using covariant parameter or return types
142
+
- In **C#** when implementing interfaces, you cannot override members or change return types
143
+
-**C#** only supports covariant overrides to `readonly` properties
144
+
- Both **Java** and **C#** do not support omitting parameters when overriding or implementing a method
145
+
- In **C#** you cannot override method parameters
144
146
145
-
!!! note
146
-
**C# 9** introduces support for covariant return types, which would allow relaxing this restriction, however `jsii`
147
-
must build code targetting the `.NET Core 3.1` runtime, which only supports **C# 8**. Once `.NET Core 3.1` becomes
148
-
end-of-life, this may change.
147
+
As a consequence, changes to member signatures are only allowed in very few cases.
148
+
All overrides must be covariant to the parent type.
149
+
150
+
- Return type of (non-static) methods
151
+
-`readonly` properties
152
+
153
+
```ts hl_lines="10-11 13-14 16-17 19-20 22-23"
154
+
exportclassSuperClass {}
155
+
exportclassSubClassextendsSuperClass {}
149
156
150
-
```ts hl_lines="6-7 9-10 12-13"
151
157
exportclassBase {
152
-
public method(param:any):any { /* ... */ }
158
+
publicreadonly prop:SuperClass;
159
+
public method(param:SuperClass):SuperClass { /* ... */ }
153
160
}
154
161
155
162
exportclassChildextendsBase {
156
163
// 💥 Parameter signatures do not match
157
164
public method():any { /* ... */ }
158
165
159
166
// 💥 Parameter types do not match, even though they are covariant
160
-
public method(param:string):any { /* ... */ }
167
+
public method(param:SubClass):any { /* ... */ }
168
+
169
+
// 💥 Property types do not match
170
+
publicreadonly prop:string;
171
+
172
+
// ✅ Return type is covariant
173
+
public method(param:SuperClass):SubClass { /* ... */ }
161
174
162
-
//💥 Return type does not match, even though it is covariant
163
-
publicmethod(param:any):string { /* ... */ }
175
+
//✅ Property is readonly and override type is covariant
176
+
publicreadonly prop:SubClass;
164
177
}
165
178
```
166
179
167
180
## Index Signatures
168
181
169
-
**TypeScript** allows declaring _additional properties_ through the use of index signatures. These are however not
170
-
supported by the _jsii type system_ and are rejected by the compiler.
182
+
**TypeScript** allows declaring *additional properties* through the use of index signatures. These are however not
183
+
supported by the *jsii type system* and are rejected by the compiler.
171
184
172
185
!!! info
173
-
Version `1.x` of the compiler _silently ignores_ index signatures instead of reporting a compilation error. Users
186
+
Version `1.x` of the compiler *silently ignores* index signatures instead of reporting a compilation error. Users
174
187
with offending APIs migrating from `jsii@1.x` to `jsii@5.0` or newer need to either remove those declarations, or
175
188
explicitly ignore them using the [`@jsii ignore` tag](./hints.md#excluding-a-declaration-from-multi-language-support).
0 commit comments