Typescript 类型为(…)的参数不能分配给类型为';绝不';
我想使用数组函数Typescript 类型为(…)的参数不能分配给类型为';绝不';,typescript,Typescript,我想使用数组函数包括。但是内部参数“searchElement”的类型为never。这对我来说毫无意义,因为我可以对代码完成和无类型错误进行单个=比较: export default async function getValidationObjectForEnergyType(energyType: keyof IdentificatorToEnergytypeMapping): Promise<void> { const energyTypeIdentificators:
包括
。但是内部参数“searchElement”的类型为never
。这对我来说毫无意义,因为我可以对代码完成和无类型错误进行单个=
比较:
export default async function getValidationObjectForEnergyType(energyType: keyof IdentificatorToEnergytypeMapping): Promise<void> {
const energyTypeIdentificators: IdentificatorToEnergytypeMapping = {
Strom: [
"contactInformationLabel",
"contractNumberLabel",
"emailLabel",
"phoneLabelPower",
"zipLabelPower"
],
Gas: [
"contactInformationLabelGas",
"contractNumberLabelGas",
"emailLabelGas",
"phoneLabelGas",
"zipLabelGas"
],
};
// this is okay
energyTypeIdentificators[energyType][0] == "emailLabel";
// but this is not ?!
energyTypeIdentificators[energyType].includes("emailLabel")
}
// Types:
interface IdentificatorToEnergytypeMapping {
Strom: MrInputKeysPower[];
Gas: MrInputKeysGas[];
}
type MrInputKeysPower = keyof MrInputsPower;
type MrInputKeysGas = keyof MrInputsGas;
interface MrInputsPower {
contactInformationLabel: string;
contractNumberLabel: string;
emailLabel: string;
phoneLabelPower: string;
zipLabelPower: string;
}
interface MrInputsGas {
contactInformationLabelGas: string;
contractNumberLabelGas: string;
emailLabelGas: string;
phoneLabelGas: string;
zipLabelGas: string;
}
也许我弄错了,但这感觉像是一个打字错误
当我使用
energyType:string
作为参数时,部件energyTypeIdentificators[energyType].includes(“emailLabel”)
的行为与预期的一样。在检查代码后,我想,我知道为什么会发生这种情况。本质上,const energyTypeIdentificators:IdentificatorToEnergytypeMapping
有两个数组作为属性。两者的类型也是一个接口数组(MrInputKeysPower
和MrInputKeysGas
),它们不是字符串。因此,当您传递一个字符串
以使用includes
进行搜索时,总是“从不”
,因为您试图将字符串
与特定接口(MrInputKeysPower
或MrInputKeysGas
)进行比较,而该字符串从来都不是字符串
。比较=
仅对界面中任何属性的名称执行为true,如果它不是名称,则会发生此错误:
This condition will always return 'false' since the types '"contactInformationLabel" | "contractNumberLabel" | "emailLabel" | "phoneLabelPower" | "zipLabelPower" | "contactInformationLabelGas" | "contractNumberLabelGas" | "emailLabelGas" | "phoneLabelGas" | "zipLabelGas"' and '"foo"' have no overlap.
因此,您应该通过以下方式检查电子邮件:
if(energyTypeIdentificators[energyType][“emailLabel”]){
}
或
if(energyTypeIdentificators[energyType].emailLabel){
}
甚至您也可以使用未定义的进行检查:
energyTypeIdentificators[energyType][“emailLabel”]==未定义
在检查了您的代码之后,我想,我知道为什么会发生这种情况。本质上,const energyTypeIdentificators:IdentificatorToEnergytypeMapping
有两个数组作为属性。两者的类型也是一个接口数组(MrInputKeysPower
和MrInputKeysGas
),它们不是字符串。因此,当您传递一个字符串
以使用includes
进行搜索时,总是“从不”
,因为您试图将字符串
与特定接口(MrInputKeysPower
或MrInputKeysGas
)进行比较,而该字符串从来都不是字符串
。比较=
仅对界面中任何属性的名称执行为true,如果它不是名称,则会发生此错误:
This condition will always return 'false' since the types '"contactInformationLabel" | "contractNumberLabel" | "emailLabel" | "phoneLabelPower" | "zipLabelPower" | "contactInformationLabelGas" | "contractNumberLabelGas" | "emailLabelGas" | "phoneLabelGas" | "zipLabelGas"' and '"foo"' have no overlap.
因此,您应该通过以下方式检查电子邮件:
if(energyTypeIdentificators[energyType][“emailLabel”]){
}
或
if(energyTypeIdentificators[energyType].emailLabel){
}
甚至您也可以使用未定义的进行检查:
energyTypeIdentificators[energyType][“emailLabel”]==未定义
那么为什么我不能使用energyTypeIdentificators[energyType][0]=“foo”代码>类型脚本给了我一个错误。所以总是执行比较==是不对的,对吗?是的,你是对的,我错了。我更新答案谢谢。但我不理解这一部分:它们不是字符串
接口的属性是字符串。这就是为什么我可以使用与==
的比较。而includes
正在数组中搜索仅包含字符串的元素。我仍然不明白为什么我不能使用includes
直接从lib.es2016.array.include.d.ts提取includes(searchElement:t,fromIndex?:number):布尔
T是MrInputKeysPower
或MrInputsGas
在您的例子中不是字符串,这是使用泛型最棒的部分。您正在尝试使用包含
混合类型。您不是在检查界面中是否存在“emailLabel”
,而是在检查数组中是否存在字符串“emailLabel”
。为什么我不能使用energyTypeIdentificators[energyType][0]=“foo”代码>类型脚本给了我一个错误。所以总是执行比较==是不对的,对吗?是的,你是对的,我错了。我更新答案谢谢。但我不理解这一部分:它们不是字符串
接口的属性是字符串。这就是为什么我可以使用与==
的比较。而includes
正在数组中搜索仅包含字符串的元素。我仍然不明白为什么我不能使用includes
直接从lib.es2016.array.include.d.ts提取includes(searchElement:t,fromIndex?:number):布尔
T是MrInputKeysPower
或MrInputsGas
在您的例子中不是字符串,这是使用泛型最棒的部分。您正在尝试使用包含
混合类型。您不是在检查界面中是否存在“emailLabel”
,而是在检查数组中是否存在字符串“emailLabel”