最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property 'fullName' implicitly has type 'any', because its set accessor lacks a par

programmeradmin0浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

the "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.

发布评论

评论列表(0)

  1. 暂无评论