admin管理员组文章数量:1427287
I was following a course in JS. And he used the following code but when I added it its not working. It returns undefined. The problem is at "set fullName" This is the error from VS code:
"Property 'fullName' implicitly has type 'any', because its set accessor lacks a parameter type annotation"
The console gives undefined.
I googled it saw something with typescript version, but I am using the latest version that is provided with VSCODE the version of TypeScript is 3.9.0
class Person {
constructor(firstname, lastName, age, likes = []) {
this.firstName = firstname
this.lastName = lastName
this.age = age
this.likes = likes
}
getBio() {
let bio = `${this.firstName} is ${this.age}.`
this.likes.forEach( (like) => {
bio = bio + ` ${this.firstName} likes ${like}.`
})
return bio
}
set fullName(fullName) {
const names = fullName.split(' ')
this.firstName = names[0]
this.lastName = names[1]
}
}
I was following a course in JS. And he used the following code but when I added it its not working. It returns undefined. The problem is at "set fullName" This is the error from VS code:
"Property 'fullName' implicitly has type 'any', because its set accessor lacks a parameter type annotation"
The console gives undefined.
I googled it saw something with typescript version, but I am using the latest version that is provided with VSCODE the version of TypeScript is 3.9.0
class Person {
constructor(firstname, lastName, age, likes = []) {
this.firstName = firstname
this.lastName = lastName
this.age = age
this.likes = likes
}
getBio() {
let bio = `${this.firstName} is ${this.age}.`
this.likes.forEach( (like) => {
bio = bio + ` ${this.firstName} likes ${like}.`
})
return bio
}
set fullName(fullName) {
const names = fullName.split(' ')
this.firstName = names[0]
this.lastName = names[1]
}
}
Share Improve this question edited Apr 13, 2020 at 15:29 Always Learning 5,6012 gold badges20 silver badges35 bronze badges asked Apr 13, 2020 at 15:20 iWBMiWBM 771 silver badge6 bronze badges1 Answer
Reset to default 4the "problem" lies in your tsconfig.
you have a file in your project root called tsconfig.json
that has this line in it:
noImplicitAny: "true"
if you don't have a tsconfig.json
, then vscode probably runs with a default config that has this option, and you can create a tsconfig to set the option.
this means that you can't write code where the type is implicitly any, you need to explicitly declare it's type or write it in a way where it's type can inferred. so you can either switch that option to false
(NOT remended) or you can explicitly declare your types
set fullName(fullName: string) {
and you probably want to get in the habit of doing this everywhere:
constructor(firstname: string, lastName: string, age: number, likes: string[] = []) {
just a personal note, course sounds out of date or not that great for typescript if they're coding with implicit any allowed.
本文标签:
版权声明:本文标题:javascript - Property 'fullName' implicitly has type 'any', because its set accessor lacks a par 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745492337a2660656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论