Skip to content

Type assertion of readonly array property doesn't work only with a class type (static property).Β #61719

Closed
@miguel-leon

Description

@miguel-leon

πŸ”Ž Search Terms

type assertion readonly array

πŸ•— Version & Regression Information

Latest as of today.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgHIHsoFs4BsCCUUcAnsgN4BQAkHEQFzIDOYUoA5gNoC6A3JQF9KwgPQAqZAFppydACMAVhARhkYEgAcU0ycjEjhAE2W46KBOhAtkcRuRsNkUCHEOXcZFmxBduyAbzIIiI2uEzoyADumADWTFHAYAAWNiDIoJCwiChJ0BAAdJQAFHA28RjYeITEJACUgcFRsfGAPBuAkfvClOJSMiBwWBBMGtk9uvpGJmbIfQNDI6gUNBAAHhqYqhZWqnRQjM6u7p6sHDz8QkULcOWYOAREpPVBIdFQccjtEwimzsib1iCMdRadAwND8IppK5oG5Ve51BrPZrvDqiCQ6PQGSjGL5THFMeIAMXQESo1BYcDAwAQDl2ThcbhAHmYxx8pxoOz29MOzO8vjOqNG6S2cBASEF4yxkx+f1UwEYRPQ4OAZWhlTuNUejRebw+XTRMjx8XREqKCpVFVu1QeCOQbkGIAA5KptchADLkn2+5ks1gQgM0EBByAV4OpUItsI1NrtTEdztibsoQA

πŸ’» Code

interface NormalArray {
	arr: string[];
}


/* --- object type --- */

declare const a: { arr: readonly string[] }; // also works with an interface here.
(a as NormalArray); // works πŸŽ‰


/* --- namespace --- */

declare namespace N {
	export const arr: readonly string[];
}
(N as NormalArray); // works πŸŽ‰

declare const n: typeof N;
(n as NormalArray); // works πŸŽ‰


/* --- */

declare class Foo {
	static arr: readonly string[];
	arr: readonly string[];
}

/* --- instance --- */

declare const i: Foo;
(i as NormalArray); // works πŸŽ‰

/* --- class --- */

(Foo as NormalArray); // doesn't work ❌

declare const c: typeof Foo;
(c as NormalArray); // doesn't work ❌

πŸ™ Actual behavior

The type assertion works with everything except on static properties.

πŸ™‚ Expected behavior

Works with everything?

Additional information about the issue

Is there a special reason for this not to work with static properties? Or should this work in all cases? Or should this not work in any case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions