1

In my list component, I am getting a model and assigning to array. But getting an error as :

Type '{ setup: string; punchline: string; hide: true; }' is not assignable to type 'Joke'.
Property 'toggle' is missing in type '{ setup: string; punchline: string; hide: true; }'.

here something going wrong. any one help me to fix this issue?

here is my model: (joke.ts)

export class Joke {
    public setup:string;
    public punchline:string;
    public hide:boolean;

    constructor(setup:string,punchline:string){
        this.setup = setup;
        this.punchline = punchline;
        this.hide = true;
    }

    toggle(){
        this.hide = !this.hide;
    }
}

Here is my component: ( where I am getting error at this.jokes )

import { Component, OnInit } from '@angular/core';
import { Joke } from '../domain-models/joke';

@Component({
    selector: 'joke-list',
    templateUrl: './joke-list.component.html',
    styleUrls: ['./joke-list.component.css']
})
export class JokeListComponent {

    jokes:Joke[];

    constructor(){
        this.jokes = [
            {
                setup: "What did the cheese say when it looked in the mirror?",
                punchline: "Hello-Me (Halloumi)",
                hide:true
            },
            {
                setup: "What kind of cheese do you use to disguise a small horse?",
                punchline: "Mask-a-pony (Mascarpone)",
                hide:false
            },
            {
                setup: "A kid threw a lump of cheddar at me",
                punchline: "I thought ‘That’s not very mature’",
                hide:true
            }
        ];
    }

}

what is wrong here? any one correct me please?

2 Answers 2

2

Use the new operator when using a class. Like this.

constructor(){
    this.jokes = [
        new Joke("What did the cheese say when it looked in the mirror?", "Hello-Me (Halloumi)"),
        new Joke("What kind of cheese do you use to disguise a small horse?", "Mask-a-pony (Mascarpone)")
    ];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well, an interface would not support a toggle method
With this solution, you can't set hide = false. That's why I think, my answer below is the right answer. @3gwebtrain
1

Use this,

Joke.ts

export class Joke {
    public setup:string;
    public punchline:string;
    public hide:boolean;

    constructor(setup:string,punchline:string, hide:boolean = true){
        this.setup = setup;
        this.punchline = punchline;
        this.hide = hide;
    }

    toggle(){
        this.hide = !this.hide;
    }
}

Component

constructor(){

this.jokes = [
        new Joke(
             "What did the cheese say when it looked in the mirror?",
             "Hello-Me (Halloumi)"
        ),
         new Joke(
            "What kind of cheese do you use to disguise a small horse?",
             "Mask-a-pony (Mascarpone)",
            false
         ),
        new Joke(
            "A kid threw a lump of cheddar at me",
            "I thought ‘That’s not very mature’"
        )
    ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.