英文:
Migrating to Typescript from React.Js New to TS learning as I go
问题
Error 1:
> Property 'value' does not exist on type 'never'.
错误 1:
> 属性 'value' 不存在于类型 'never' 上。
Error 2:
I get the Error on this one with .value on the middle line again. (breed.value), just the .value gives me the error though.
错误 2:
我在这个地方再次遇到了错误,中间行的 .value (breed.value) 给我错误,但只是 .value 给我错误。
英文:
I have 2 errors that say the exact thing and I can figure out what I need to place for them to be correct and clear up.
Error:
> Property 'value' does not exist on type 'never'.
useEffect(() => {
// @ts-expect-error -- TODO: Variable 'cities' implicitly has type 'any[]' in some locations where its type cannot be determined.
const cities = [];
selectedStates.forEach((state) =>
cities.push(...getCityByState(state.value).map((city) => city.name)) // Error on .value
);
// @ts-expect-error -- TODO: Variable 'cities' implicitly has an 'any[]' type.
setCities(cities);
}, [selectedStates]);
The Error is for the .value in the middle line. (state.value), just the .value gives me the error though.
Here is the other area I get the same Error:
const getSearchQuery = (zipCodes: string[]) => {
let query = Default_Search + `sort=breed:${selectedSort.value}&`;
query += selectedBreeds
.map((breed) => `dogBreeds=${breed.value}&`) //Error .value
.join("");
query += zipCodes.map((zipcode) => `zipCodes=${zipcode}&`).join("");
return query + `ageMin=${minAge.value}&ageMax=${maxAge.value}&`;
};
I get the Error on this one with .value on the middle line again. (breed.value), just the .value gives me the error though.
I am not sure what I am missing with setting this type.
答案1
得分: 1
以下是已翻译的内容:
你必须为state分配类型,像这样,
const type State = {
value: string;
}
const type City = {
name: string;
}
useEffect(() => {
const cities = [] as City[];
selectedStates.forEach((state: State) =>
cities.push(...getCityByState(state.value).map((city: City) => city.name)))
setCities(cities);
}, [selectedStates]);
这是TypeScript,所以你必须为变量分配类型;否则,会出现类似这样的错误。
对于第二个也要做同样的事情。
英文:
You have to assign type to state, like,
const type State = {
value: string;
}
const type City = {
name: string;
}
useEffect(() => {
const cities = [] as City[];
selectedStates.forEach((state: State) =>
cities.push(...getCityByState(state.value).map((city: City) => city.name)))
setCities(cities);
}, [selectedStates]);
This is typescript, so you have to assign type to the variable; otherwise, it'll give errors like this.
You have to do the same for the second one.
答案2
得分: 0
你遇到这些错误是因为两个原因:
-
你可能将变量 "state" 声明为空对象;
所以,类型推断无法发挥作用,因为关于 "state" 的信息不多。 -
你没有为 "state" 做类型声明。这在 TypeScript 尝试访问仅你作为开发者知道将填充对象的属性(例如 "value")时会引发冲突。
如何解决?
type City = {
name: string;
// 其他属性...
}
type State = {
value: Array<City>;
}
useEffect(() => {
const cities: Array<string> = []; /* 你将使用城市名称填充此数组 */
selectedStates.forEach((state: State) =>
cities.push(...getCityByState(state.value).map((city: City) => city.name))
);
setCities(cities);
}, [selectedStates]);
英文:
You're getting those errors because of two things:
-
You probably declared the variable "state" as an empty Object;
So, the type inference can't do its magic because there's not much information about "state". -
You didn't make a type declaration for "state". That's conflictive for TS when trying to access properties that only you as dev know will populate your object such as "value".
How to solve it?
type City = {
name: string;
...etc
}
type State = {
value: Array<City>;
}
useEffect(() => {
const cities:Array<string> = []; /*You will be populating this arr with names of cities */
selectedStates.forEach((state: State) =>
cities.push(...getCityByState(state.value).map((city: City) => city.name)))
setCities(cities);
}, [selectedStates]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论