I would like to define type when destructuring an array, But it shows some error:
interface buildTargetProps {
target: string;
path: string;
}
interface buildTargets {
[property: string]: buildTargetProps
};
const ChooseApp: buildTargets = {
'autocomplete': {
target: "test",
path: "./Autocomplete",
},
'search': {
target: "search-root",
path: "./Search",
},
};
let [applicationToBuild] = Object.entries(ChooseApp).find(
([name, props]: [String, buildTargetProps]) => {
if (document.getElementById(props.target)) {
return name;
}
}
);
What I want to do is defined the type of "applicationToBuild" variable, for that I have Tried:
let [applicationToBuild]: [string] | undefined = Object.entries(ChooseApp)
I intentionally skipped the other array element, as I don't need that, But I tried to add that as well, to check if that resolves the error, But that also didn't work.
let [applicationToBuild, otherprops]: [string, buildTargetProps] | undefined = Object.entries(ChooseApp)
But, this throws an error.
Type '[string, buildTargetProps] | undefined' is not assignable to type '[string] | undefined'.
Type '[string, buildTargetProps]' is not assignable to type '[string]'.
Source has 2 element(s) but target allows only 1.ts(2322)
Type '[string] | undefined' is not an array type.ts(2461)
question from:
https://stackoverflow.com/questions/65902627/define-type-when-destructuring-an-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…