0

I'm new to typescript plz help

I'm struggling to understand the error message for the below code. I'm just trying to define an array of objects and can't seem to figure out why it's not working?

interface FieldClass {
  id: string,
    name: string,
    type: string,
    label: string
}

const fields: FieldClass[] = [{
  id: 'name',
  name: 'input',
  type: 'name',
  label: 'Name',
}, {
  id: 'email',
  name: 'email',
  type: 'email',
  label: 'Email',
}, {
  id: 'password',
  name: 'password',
  type: 'password',
  label: 'Password',
}, , {
  id: 'confirm-password',
  name: 'confirm-password',
  type: 'confirm-password',
  label: 'Confirm password',
}];

Error message:

Type '({ id: string; name: string; type: string; label: string; } | undefined)[]' is not assignable to type 'FieldClass[]'.
  Type '{ id: string; name: string; type: string; label: string; } | undefined' is not assignable to type 'FieldClass'.
    Type 'undefined' is not assignable to type 'FieldClass'.  TS2322
2
  • 1
    Looks fine so far. Just fix , , in fields and in your interface id: string; (notice ; instead of ,) and so on. Commented Feb 18, 2020 at 15:15
  • @pzaenger Thanks, that was the problem! Commented Feb 18, 2020 at 15:17

1 Answer 1

1

You have a type error here:

  label: 'Password',
}, , {
  id: 'confirm-password',

Should be with one comma

  label: 'Password',
}, {
  id: 'confirm-password',
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it was going to be something stupid like that. Thank you

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.