-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
const type generic parameter class anonymous
π Version & Regression Information
- I was unable to test this on prior versions because it is a TypeScript 5 new feature.
β― Playground Link
π» Code
declare function test<const V extends object>(value: V): V;
const valid = test({ foo: ['bar'] });
const staticInvalid = test(class {
// Error: TS7022 'foo' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
static readonly foo = ['bar'];
});
const instanceInvalid = test(class {
readonly foo = ['bar'];
});
// Invalid type: string[] instead of ['bar']
new instanceInvalid().fooπ Actual behavior
TS7022error appears on the staticfooproperty.- Type is
string[]on the instancefooproperty.
π Expected behavior
No error should be emitted, and static/instance foo properties should have the same type as with an object literal: ['bar'].
Additional information about the issue
In practice, both object literal and class static array properties could be changed.
But if const type parameters work on object literal, why not on classes static/instance properties?
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created