Cannot assign readonly statics in static blocks
@rbuckton đang làm issue này rồi.
Từ ngày 29/11/2023.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
🔍 Search Terms
"assign readonly static in static block", "assign readonly static in static constructor"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
static readonly fields should be assignable within static code blocks, similar to how readonly fields are assignable in constructors. I've had a read through various issues, and those around the MR that introduced support for static blocks, but found no mention of dealing with readonly fields.
📃 Motivating Example
I'm making a variation on ringbufferjs, here, this.headerSize = ... shows an error without the //@ts-expect-error
type TypedArray =
| Float32Array
| Float64Array
| Int8Array
| Int16Array
| Int32Array
| BigInt64Array;
type TypedArrayConstructor =
| Float32ArrayConstructor
| Float64ArrayConstructor
| Int8ArrayConstructor
| Int16ArrayConstructor
| Int32ArrayConstructor
| BigInt64ArrayConstructor;
export default class LiveRingBuffer {
private static readonly headerSize: number;
static {
const writePtrWidth = Uint32Array.BYTES_PER_ELEMENT;
const readPtrWidth = Uint32Array.BYTES_PER_ELEMENT;
const lapPtrWidth = Uint32Array.BYTES_PER_ELEMENT;
//@ts-expect-error
this.headerSize = writePtrWidth + readPtrWidth + lapPtrWidth;
}
static getStorageForCapacity(
capacity: number,
type: TypedArrayConstructor,
): SharedArrayBuffer {
if (!type.BYTES_PER_ELEMENT) {
throw TypeError('Pass in a ArrayBuffer subclass');
}
const storageWidth = (capacity + 1) * type.BYTES_PER_ELEMENT;
const alignedHeaderWidth =
Math.ceil(this.headerSize / type.BYTES_PER_ELEMENT) *
type.BYTES_PER_ELEMENT;
const bytes = alignedHeaderWidth + storageWidth;
return new SharedArrayBuffer(bytes);
}
// ...
}
💻 Use Cases
- What do you want to use this for?
See "Motivating Example" - What shortcomings exist with current approaches?
Current approach suppresses readonly error; alternative is to remove thereadonlykeyword, also not ideal since it really is readonly/const - What workarounds are you using in the meantime?
//@ts-expect-errorhides the error and emits the desired code.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/TypeScript
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64322 · 2 bình luận · 1 reaction · 2 người được giao ·
-
Possible Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
microsoft/TypeScript#64278 · 1 bình luận · 1 reaction ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/TypeScript#64118 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64094 ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/TypeScript#63959 · 5 bình luận ·
Tất cả issue của microsoft/TypeScript
Issue tương tự
-
optimization optimization:agents-md-curator
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
githubnext/gh-aw-cao#13143 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
blinklabs-io/bursa#904 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 bình luận ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100