Skip to content

Commit d9f8334

Browse files
committed
docs
1 parent 8b209be commit d9f8334

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

gh-pages/content/user-guides/lib-author/typescript-restrictions.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ coexist in the same closure, it is recommended to also declare all such dependen
1818

1919
Occasionally, a dependency on a *non-jsii module* is useful. Since such dependencies do not have generated bindings in
2020
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`.
2222

2323
The API of the *jsii module* can not expose any type from bundled dependencies, since those types would not be available in other languages.
2424
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 {
137137
### Covariant Overrides & Parameter List Changes
138138

139139
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:
141141

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+
- **C#** does not support overriding or members or changing return types when implementing interfaces
143+
- **Java** and **C#** do not support omitting parameters when overriding or implementing a method
144+
- **C#** does not support overrides of method parameters
145+
- **C#** only supports covariant overrides to `readonly` properties
144146

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+
export class SuperClass {}
155+
export class SubClass extends SuperClass {}
149156

150-
```ts hl_lines="6-7 9-10 12-13"
151157
export class Base {
152-
public method(param: any): any { /* ... */ }
158+
public readonly prop: SuperClass;
159+
public method(param: SuperClass): SuperClass { /* ... */ }
153160
}
154161

155162
export class Child extends Base {
156163
// 💥 Parameter signatures do not match
157164
public method(): any { /* ... */ }
158165

159166
// 💥 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+
public readonly prop: string;
171+
172+
// ✅ Return type is covariant
173+
public method(param: SuperClass): SubClass { /* ... */ }
161174

162-
// 💥 Return type does not match, even though it is covariant
163-
public method(param: any): string { /* ... */ }
175+
// ✅ Property is readonly and override type is covariant
176+
public readonly prop: SubClass;
164177
}
165178
```
166179

167180
## Index Signatures
168181

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.
171184

172185
!!! 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
174187
with offending APIs migrating from `jsii@1.x` to `jsii@5.0` or newer need to either remove those declarations, or
175188
explicitly ignore them using the [`@jsii ignore` tag](./hints.md#excluding-a-declaration-from-multi-language-support).
176189

0 commit comments

Comments
 (0)