I have some vanilla javascript code that takes a string input, splits the string into characters, and then matches those characters to a key on an object.
DNATranscriber = {
"G":"C",
"C": "G",
"T": "A",
"A": "U"
}
function toRna(sequence){
const sequenceArray = [...sequence];
const transcriptionArray = sequenceArray.map(character =>{
return this.DNATranscriber[character];
});
return transcriptionArray.join("");
}
console.log(toRna("ACGTGGTCTTAA")); //Returns UGCACCAGAAUU
This works as expected. I'd now like to convert this to typescript.
class Transcriptor {
DNATranscriber = {
G:"C",
C: "G",
T: "A",
A: "U"
}
toRna(sequence: string) {
const sequenceArray = [...sequence];
const transcriptionArray = sequenceArray.map(character =>{
return this.DNATranscriber[character];
});
}
}
export default Transcriptor
But I'm getting the following error.
Element implicitly has an 'any' type because expression of type 'string' >can't be used to index type '{ "A": string; }'.
No index signature with a parameter of type 'string' was found on type >'{ "A": string; }'.ts(7053)
I thought that the issue was that I needed my object key to be a string. But converting them to strings didn't work.
DNATranscriber = {
"G":"C",
"C": "G",
"T": "A",
"A": "U"
}
I'm quite confused by this. It says that no index signature with a type of string exists on my object. But I'm sure that it does. What am I doing wrong?
Edit - I solved this by giving the DNATranscriber object a type of any.
DNATranscriber: any = {
"G":"C",
"C":"G",
"T":"A",
"A":"U"
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…