aws / aws/jsii-compiler

Typescript's readonly arrays should be allowed

Open
#2,323 0 comments 0 reactions 0 assignees View on GitHub
p2
Dominant language
TypeScript
Stars
50
Forks
26
Avg merge
7h 10m
Merged PRs (30d)
37

Description

### Describe the bug

An interface
```
export interface Props {
readonly myArray: readonly string[];
}
```
fails with =:
```
src/index.ts:3:17 - error JSII3001: Type "ReadonlyArray" cannot be used as the property type because it is private or @internal
```

preventing using it as
```
const arr = ["a","b"] as const
```

### Expected Behavior

It should compile, ReadonlyArray<> is a TS type.

### Current Behavior

`src/index.ts:3:17 - error JSII3001: Type "ReadonlyArray" cannot be used as the property type because it is private or @internal`

### Reproduction Steps

```
export interface Props {
readonly p: readonly string[];
}

export class C {
private s: readonly string[];
constructor(p: Props) {
this.s = p.p;
}
f() {
return this.s;
}
}
```

### Possible Solution

Map ReadonlyArray to a plain array in languages not supporting readonly.

### Additional Information/Context

_No response_

### SDK version used

latest

### Environment details (OS name and version, etc.)

Linux

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in src/index.ts and run the compiler against the readonly string[] property and readonly tuple usage shown in the issue. Trace how ReadonlyArray is classified and verify that the interface compiles while readonly arrays are represented appropriately in languages without readonly support.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.