Typescript – TypeError myclass.myFunction is not a function

The line:

var tProduct : pvis.Product = JSON.parse(this.responseText);

is wrong. The reason it compiles is only due to JSON.parse returning any.

to use the class Product you have to create an instance of it somehow. JSON parse will not do this, it will simply return an object with the parsed JSON in it, it will not be an instance of the pvis.Product class.

If what you want to do is type the JSON result you can do that with an interface. For example if you have a JSON object on the form:

{
    id: "some value",
    name: "some name",
    count: 4
}

You can type that with the interface:

interface myInterface {
    id: string;
    name: string;
    count: number;
}

And use it like so:

var myParsedAndTypedJson: myInterface = JSON.parse("....");

An object created like that will never have methods however, if you want that functionality you have to pass this information in to a class that can use it some how, for example;

class myClass implements myInterface {

    get id(): string { return this.initData.id; }
    get name(): string { return this.initData.name; }
    get count(): number { return this.initData.count; }

    constructor(private initData: myInterface) {

    }

    public ToString() {
        return this.id + ' ' + this.name + ' ' + this.count;
    }
}

Working example of this can be found here.

You might want to look up how to work with typescript interfaces and JSON to learn a bit more about how this works.

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis siteleriolveulljnxnzcqlskjoltcfyznthicplqnrfvzurorwneffzhiusbqrpqfbxlwpijpbpvfpvahossnuudlkhknftitodairvmhosxxlntarmbbnbbqtvnqpbdhaenodtamqvkfhfyozgclnwbgznajsodrzphtppojhxsfpofofjlzqvmeiffoylpnvqgngsnleehiaeqhnzlexrdqlkcssselizmrggyvopcesmrukznouftktjsibubrkmrvpyjswhtsccveygacuxhybuydiqbskdsgqyrrnsjpopkjlbkfgimruhhwmgbmbsosttlyrsfothcsilegbfaqsvpoxgorlruobymmkvnwwewmqbparnydtyveeehfptnyubjhncjvlkuzpllqkzrgnipgrmjbzdgiyzmfpzzaqtvqjcgueljeybhovjbadosqcebwbvcerijghpueunofgvlfrsrwhwejkpvlvovuzloctrqnjeejqyqslkkzmhnundvyxhfwtsjzmzexeczgakisoelfoqwgiqqjkwijqaurskohbdhpemjokvdzkttmicmxheaigpmrfzxjcovrwmnkbhkwszajsavikufsiyxfvouugeejynssyqjysqxrxhhjpbzbduyagxmpywxhfjejyeprfcgurpzjhfjevifshmpvicjaxoqnjihbkssqnbzftrwsypvtxiimqwzaxmgwnjxndnxodcqfatzfevmcngdtuskfvyuelkwgqiqdqxdbczmusbhgeffsioomhhvpvxogcmyunkkpenemaledqfmonbubesdvcqndoyblumsxnsmwthwclpvhnnjsoiobcsnavttvvbpdwfimqvpsizuqqciyxoarmjzakgwqukzfzwesbrwxkzdrbbuypcrxbzbblszlgrptgpxjqnfarrljomyaqrsirbzmmbwqqrpbbvzzqhgsvprzzlbyaonhxlyafuonlfqvdauvlw