Skip to content

String indexer is readonly in lib._x_.d.ts #10097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
david-driscoll opened this issue Aug 2, 2016 · 3 comments
Closed

String indexer is readonly in lib._x_.d.ts #10097

david-driscoll opened this issue Aug 2, 2016 · 3 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@david-driscoll
Copy link

Currently the string indexer is currently set to readonly in lib.d.ts. When trying to swap out characters in a string, you must cast to any to solve. Is this expected behavior, or a side effect of some other issue being solved?

This is not an issue for Array, as it's indexer is mutable.

https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es5.d.ts#L442
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts#L442
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts#L442

TypeScript Version: 1.8.0 / nightly (2.0.0-dev.201xxxxx)
2.1.0-dev.20160801
Code

let rangeValue = '1234567890123'
if (rangeValue.length > 1) {
    rangeValue[0] = 'V';
}  

Expected behavior:
Should be able to assign a value to any index of a string;

Actual behavior:
Get compiler error Left-hand side of assignment expression cannot be a constant or a read-only property.

@RyanCavanaugh
Copy link
Member

JS strings are immutable. The code you have there doesn't work.
image

@DanielRosenwasser DanielRosenwasser added the External Relates to another program, environment, or user action which we cannot control. label Aug 2, 2016
@david-driscoll
Copy link
Author

Yeah I was seeing some strange behavior in chrome, but yeah they're not mutable normally. I'll go back to work. 🐑 Thanks @RyanCavanaugh

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

3 participants