0

My problem is related to Angular @Input() decorator, as when I am using this decorator, typescript throwing error, not when using in regular code.

In my child.component.ts file I am declaring this decorator to get props from parent component :

@Input() customColumns: {
    name: string;
    index: number;
    type: 'icon' | 'image' | 'link' | 'button';
    icon?: any;
    url?: string;
  }[] = [];
  indexList: number[] = [];

And in my parent.component.ts file I am assigning the value for this variable like this :

customColumns = [
    { name: 'permissions', index: 7, type: 'icon', icon: faSave },
    { name: 'price list', index: 6, type: 'link', icon: faArrowDownWideShort },
    { name: 'details', index: 5, type: 'button', icon: faArrowUpWideShort },
  ];

Lastly, in my parent.component.html file I am calling that child component:

<app-child [customColumns]="customColumns">
</app-child>

But I'm getting this error:

Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"button" | "link" | "image" | "icon"'.

But when I am doing same thing in normal typescript or ngOnInit() function it is working, can't figure out why it is happening, help me please, thanks in advance.

    let customColumns: {
      name: string;
      index: number;
      type: 'icon' | 'image' | 'link' | 'button';
      icon?: any;
      url?: string;
    }[] = [];

    customColumns = [
      { name: 'permissions', index: 7, type: 'link', icon: '' },
      {
        name: 'price list',
        index: 6,
        type: 'icon',
        icon: faArrowDownWideShort,
      },
      { name: 'details', index: 5, type: 'icon', icon: faArrowUpWideShort },
    ];

My project dependencies:

"@angular/cli": "~14.2.7",
"typescript": "~4.7.2"
2
  • You can set type?:any Commented Nov 15, 2022 at 4:44
  • I can, but I want to restrict that , others can only assign 4 options or string values. Commented Nov 15, 2022 at 4:47

2 Answers 2

2

In order to replicate the same behavior in normal TypeScript you should use this scenario:

let customColumns: {
  name: string;
  index: number;
  type: 'icon' | 'image' | 'link' | 'button';
  icon?: any;
  url?: string;
}[] = [];

const anotherValue =  [
  { name: 'permissions', index: 7, type: 'link', icon: '' },
  {
    name: 'price list',
    index: 6,
    type: 'icon',
  },
  { name: 'details', index: 5, type: 'icon', icon: '' },
];
customColumns = anotherValue;

That's simular to Angular typecheck code.

In order to solve it you can use either predefined enum:

const enum ColumnType {
  icon = 'icon',
  image = 'image',
  link = 'link',
  button = 'button',
}

let customColumns: {
  name: string;
  index: number;
  type: ColumnType;
  icon?: any;
  url?: string;
}[] = [];


...
type: ColumnType.link,

or as const:

type: 'link' as const
Sign up to request clarification or add additional context in comments.

5 Comments

type: 'link' as const, I can't do this in angular, I have to declare interface for it . My problem is not normal typescript, but it is angular @Input (), where it is not working. But thanks for your answer anyway.
Anyway? I showed behavior in simple ts but gave you Angular solutions. stackblitz.com/edit/…
But I prefer enum stackblitz.com/edit/…
The enum isn't strictly necessary, a simple typedef for the column type should be enough. typedef ColumnType = 'icon' | 'image' | 'link' | 'button';
@JeffMercado With type ColumnType = 'icon' | 'image' | 'link' | 'button'; you have to use as const or as ColumnType again stackblitz.com/edit/…
1

Please try this

1.create a class like this

export class CustomColumns {
constructor(
public name?: string,
public index?: number,
public type?: 'icon' | 'image' | 'link' | 'button',
public icon?: any,
public url?: string
){}
}

2.and in your child component use like this

@Input() customColumns: CustomColumns;

6 Comments

I already have done same thing, as you mentioned. It is working , as I described in my question, but not working , when I am using @Input () , instead of let.
I edited my answer dr. please check it now.
Happy to hear that. Can you accept my answer dr
But, he gave me stackbiltz link, so he deserve it more, I think. But you are cool also, happy coding.
Oh yes he deserve that. I just saw his answer.
|

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.