Can anyone explain to me why I need to use the code return DNAtoRNA[el as DNA]
to access the value of a Record? Why do I encounter a linting error when attempting to access it using DNAtoRNA[el]
?
I had the impression that a Record in TS was similar to a Map in JS. If that is the case, then why am I unable to utilize the get method to retrieve a value?
Appreciate any insights!
type DNA = 'G' | 'C' | 'T' | 'A';
type RNA = 'C' | 'G' | 'A' | 'U';
const DNAtoRNA: Record<DNA, RNA> = {
'G': 'C',
'C': 'G',
'T': 'A',
'A': 'U'
};
class Transcriptor {
toRna(dna: string) {
//const formatInputToArr: string[] = dna.split('');
const translateDnaToRna = dna.split('').map(el => {
return DNAtoRNA[el as DNA]
})
console.log(translateDnaToRna);
if (translateDnaToRna.includes(undefined)) {
throw new Error('Invalid input DNA.');
} else {
return translateDnaToRna.join('');
}
}
}