字符串上未找到参数类型为

字符串上未找到参数类型为

本文介绍了打字稿:在类型"{""A":字符串上未找到参数类型为“字符串"的索引签名; }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些原始的javascript代码,它们需要输入字符串,将字符串拆分为字符,然后将这些字符与对象上的键进行匹配.

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

但是出现以下错误.

我认为问题是我需要我的对象键是一个字符串.但是将它们转换为字符串是行不通的.

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?

编辑-通过为DNATranscriber对象提供任何类型的对象,我解决了这个问题.

Edit - I solved this by giving the DNATranscriber object a type of any.

DNATranscriber: any = {
    "G":"C",
    "C":"G",
    "T":"A",
    "A":"U"
}

推荐答案

您可以通过验证输入来纠正错误,无论如何,这都是您应该做的事情.

You can fix the errors by validating your input, which is something you should do regardless of course.

以下类型通过类型保护验证正确检查

The following typechecks correctly, via type guarding validations

const DNATranscriber = {
    G: 'C',
    C: 'G',
    T: 'A',
    A: 'U'
};

export default class Transcriptor {
    toRna(dna: string) {
        const codons = [...dna];
        if (!isValidSequence(codons)) {
            throw Error('invalid sequence');
        }
        const transcribedRNA = codons.map(codon => DNATranscriber[codon]);
        return transcribedRNA;
    }
}

function isValidSequence(values: string[]): values is Array<keyof typeof DNATranscriber> {
    return values.every(isValidCodon);
}
function isValidCodon(value: string): value is keyof typeof DNATranscriber {
    return value in DNATranscriber;
}

值得一提的是,您似乎误以为将JavaScript转换为TypeScript涉及使用类.

It is worth mentioning that you seem to be under the misapprehention that converting JavaScript to TypeScript involves using classes.

在以下更惯用的版本中,我们利用TypeScript来提高清晰度,并在不更改实现的情况下获得更强的碱基对映射类型.就像原始的一样,我们使用function,因为它很有意义.这个很重要!将JavaScript转换为TypeScript与类无关,而与静态类型有关.

In the following, more idiomatic version, we leverage TypeScript to improve clarity and gain stronger typing of base pair mappings without changing the implementation. We use a function, just like the original, because it makes sense. This is important! Converting JavaScript to TypeScript has nothing to do with classes, it has to do with static types.

const DNATranscriber = {
    G = 'C',
    C = 'G',
    T = 'A',
    A = 'U'
};

export default function toRna(dna: string) {
    const codons = [...dna];
    if (!isValidSequence(codons)) {
        throw Error('invalid sequence');
    }
    const transcribedRNA = codons.map(codon => DNATranscriber[codon]);
    return transcribedRNA;
}

function isValidSequence(values: string[]): values is Array<keyof typeof DNATranscriber> {
    return values.every(isValidCodon);
}
function isValidCodon(value: string): value is keyof typeof DNATranscriber {
    return value in DNATranscriber;
}

更新:

自TypeScript 3.7起,我们可以更富有表现力地编写此代码,使用断言签名将输入验证及其类型含义之间的对应关系形式化.

Since TypeScript 3.7, we can write this more expressively, formalizing the correspondence between input validation and its type implication using assertion signatures.

const DNATranscriber = {
    G = 'C',
    C = 'G',
    T = 'A',
    A = 'U'
} as const;

type DNACodon = keyof typeof DNATranscriber;
type RNACodon = typeof DNATranscriber[DNACodon];

export default function toRna(dna: string): RNACodon[] {
    const codons = [...dna];
    validateSequence(codons);
    const transcribedRNA = codons.map(codon => DNATranscriber[codon]);
    return transcribedRNA;
}

function validateSequence(values: string[]): asserts values is DNACodon[] {
    if (!values.every(isValidCodon)) {
        throw Error('invalid sequence');
    }
}
function isValidCodon(value: string): value is DNACodon {
    return value in DNATranscriber;
}

您可以在断言签名的更多信息. rel ="nofollow noreferrer"> TypeScript 3.7发行说明.

You can read more about assertion signatures in the TypeScript 3.7 release notes.

这篇关于打字稿:在类型"{""A":字符串上未找到参数类型为“字符串"的索引签名; }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:42