Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index signature in type 'String' only permits reading

Tags:

typescript

I got this warning from variable cipher when I was trying to change a string character at specified index.

const emojis: string[] = [/* values */];

function revealOriginEmojis(cipher: string): string {
  for(let i = 0; i < cipher.length; i++){
    let index: number = emojis.indexOf(cipher[i]);

    cipher[i] = emojis[index];
  }

  return cipher;
}

So, should I create a new string variable or any better solutions? Thank you so much

like image 613
Fanny Hasbi Avatar asked Apr 12 '20 00:04

Fanny Hasbi


People also ask

What is an index signature?

The idea of the index signatures is to type objects of unknown structure when you only know the key and value types. An index signature fits the case of the salary parameter: the function should accept salary objects of different structures — only that values to be numbers.

Is incompatible with index signature?

The error "Property is incompatible with index signature" occurs when a property is not compatible with the specified type of the index signature. To solve the error, change the type of the property or use a union to update the type in the index signature.

What is index type in TypeScript?

The indexing type is itself a type, so we can use unions, keyof , or other types entirely: type I1 = Person ["age" | "name"]; type I1 = string | number. type I2 = Person [keyof Person ]; type I2 = string | number | boolean.

What is index is not assignable to string type?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion. Here is the first example of how the error occurs.

When are explicit members required to conform to string index signatures?

As soon as you have a string index signature, all explicit members must also conform to that index signature. Property 'fooMethod' of type ' (fooParam: string): void' is not assignable to string index type 'string'

Why do we make index signatures readonly?

Also, we can designate index signatures as readonly so that they can be written to when the object with a type with indexable types is initialized but not after.

What are the requirements for index signature in typescript?

An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type. 3. Index signature caveats The index signatures in TypeScript have a few caveats you should be aware of.

What is the return type of string indexable signature?

If we have both number and string index signatures, then the string indexable signature must have the return type that’s the super-type of the one with the number index signature so that we get consistent types for objects when we access properties. Touched very intresting points, thanks!


1 Answers

A string is a primitive value, it's immutable.

you can convert it into array of chars, edit some elements of the array and convert back the array into string.

const cipherChars = [...cipher]; // convert into array

cipherChars[2] = 'X'; // alter array

cipher = cipherChars.join(''); // convert back into string
like image 130
Yukulélé Avatar answered Nov 16 '22 02:11

Yukulélé