You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Number index signature doesn't work with Readonly generic #17857
In addition to what olegdunkan stated, Readonly<T> and other mapped types should be applied at the point of use for correct inference, not instantiated ahead of time and used as annotations.
The type of x in var x:keyof {[k:number]:string} is never
The type of x in var x:keyof {[k:string]:string} is string
Then we can assume that in the case of a number index signature tsc produces empty type.
For string index signature next statement is true:
A mapped type takes one of the forms
{ [ P in K ] : T }
{ [ P in K ] ? : T }
{ readonly [ P in K ] : T }
{ readonly [ P in K ] ? : T }
where P is an identifier, K is a type that must be assignable to string
When P is a string literal type, a property with that name is introduced. Otherwise, when P is type string, an index signature is introduced.
Activity
olegdunkan commentedon Aug 17, 2017
Readonlyis a mapped type and requires named properties inT,Readonlyfrom number index signature is the empty type{}.(edited)
aluanhaddad commentedon Aug 17, 2017
In addition to what olegdunkan stated,
Readonly<T>and other mapped types should be applied at the point of use for correct inference, not instantiated ahead of time and used as annotations.Consider rewriting the example function as
remagpie commentedon Aug 18, 2017
olegdunkan
But when I use string index instead, I don't get any error.
Like this:
P.S. I think it should be separate issue, but I don't get any error when I do
const z: string = x[0] + y[0];too.olegdunkan commentedon Aug 18, 2017
Declaration of
ReadonlyisThe type of
xinvar x:keyof {[k:number]:string}isneverThe type of
xinvar x:keyof {[k:string]:string}isstringThen we can assume that in the case of a number index signature
tscproduces empty type.For string index signature next statement is true:
#12114 (comment)
remagpie commentedon Aug 18, 2017
Oh, it was related to mapped type's operation.
Thank you for reference.