I have problems marked in my code typescript I have no idea what should be

0

I get some errors in vs code when writing a class in typescript:

interface User{
    name: string
    userName: string
    id: number
    date: object
    UserInfoObject()
    RegisterUserData(info: object)

}
let db : any[] = []
class User implements User{
    name: string
    userName: string
    id: number
    date: object
    constructor(name: string, userName: string, id: number, date: object = new Date()){
        this.name = name
        this.userName = userName
        this.id = id
        this.date = date
    }
    UserInfoObject(){
        return {id: this.id, name: this.name, userName: this.userName, date: this.date }
    }
    RegisterUserData(info?: object){
        db.push(info || this.UserInfoObject())
    }
}

class premiumUser extends User{
    premium: boolean
    constructor(name: string, userName: string, id:number, date:Object = new Date(), premium: boolean = true){
        super(name,userName,id,date)
        this.premium = premium
    }
    PremiumUserInfo(){
        return {id: this.id, name: this.name, userName: this.userName, date: this.date, premium: this.premium}
    }
    RegisterUserData(){
        super.RegisterUserData(this.PremiumUserInfo())
    }
}

const jose = new premiumUser("jose","jose2018",1)
jose.RegisterUserData()
const victor = new User("victor", "victorl", 2)
victor.RegisterUserData()

problems that appear:

Why is this? transpila my code without any problem and works as it should. thanks in advance

    
asked by Victor Lozada 03.06.2018 в 08:21
source

2 answers

0

You have both an interface and a class called User , something that is not valid.

Try using the nomenclature I where all the interfaces begin with the letter I and you will know which is an interface and which is not. Rename the interface to IUser .

Regarding the variable scope error db and jose . This means that you can not declare a variable outside of a class. Although it is valid in javascript, this is not true in TypeScript:

var db : any; // invalido, debe de estar dentro de una clase
class MiClase{

   nombre : any; // valido, esta declarada dentro de una clase
}
    
answered by 03.06.2018 в 13:50
0

I just compiled your code and there seems to be no problem

see online example

It seems like a compiler problem (as if you were compiling the same code twice or as if the .js was already generated and you want to mix it with the .ts).

Maybe we can help you if you tell us what options the VSC has, if there is a tsconfig.json and other configurations (also the VSC version)

Transpile vs execute.

Another important issue to clarify: What type error the tsc (or the VSC) does not mean that the underlying JS code is wrong or not going to work , the only thing that means is that according to The declared types can not guarantee that these types are coherent throughout the code and / or that some of the structures used are poorly written, declared or not.

    
answered by 07.06.2018 в 02:33