1

I'm trying to define a scope variable in Angular 2. Here is the code:

export class GroceryComponent {
  task = {
    name: ''
  };
  tasks = [];
}

But its throwing the following error :

Type String is not assignable to type '{ name : string; }' 
1
  • Where is it throwing that error? Where are you using task? So many questions Commented May 22, 2017 at 11:40

2 Answers 2

2

Probably other parts of your code try to change the task property from the instance of GroceryComponent


Something similar with this error:

class GroceryComponent {
  task = {
    name : ""
  };
  tasks = [];
};

let product  = new GroceryComponent();
product.task = 55; // ERROR MESSAGE: Type '55' is not assignable to type '{ name: string; }'.

Interactive example on: TypeScript Playground

Sign up to request clarification or add additional context in comments.

Comments

0

What version are you using? I am on 1.0.0-beta.20-4 version. It's working for me.

But anyway you can also put that into a type alias:

export class GroceryComponent {
    task = {
        name: '' as string, name1: '' as string
    };
    tasks = [];
}

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.