0

enter image description here

I have an @Input() property with the data type string | string[]. Before upgrading to Angular 16, it was working fine, but after I upgraded to Angular 16, I started getting this error. I'm not sure where I went wrong. I have a component that has an input property as shown below:

let call this MyComponent 
@Input() info: string | string[];

Somewhere in my app, I am using this component and passing the info value:

// Case #1
// value = 'something in ts file'
<my-component [info]="value">
// Case #2
// arrayValue = ['a', 'b', 'c']
<my-component [info]="arrayValue">

I'm not sure what is wrong. I have already tried changing the type like this:

export type stringOrArray = string | string[];

However, I got the same error.

3
  • 2
    Can you share the code which leads to the mentioned compilation error? Pretty sure you are trying to assign it to a variable which is string type. It can't accept the string[] value.
    – Yong Shun
    Commented Apr 24, 2024 at 6:51
  • i have updated the use cases above. kindly can you check where i might be wrong. Commented Apr 24, 2024 at 7:52
  • And then how is info consumed in the component? Is there an assignment sth = someFunction(this.info), is it used in the template <some-child someInput="info">?
    – mbojko
    Commented Apr 24, 2024 at 8:29

2 Answers 2

3

Since there are two possible types string and string[] we need to explicitly tell typescript that the value you are assigning is of which type

this.test = <string>this.info;

Full code:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  standalone: true,
  imports: [],
  templateUrl: './child.component.html',
  styleUrl: './child.component.css',
})
export class ChildComponent {
  @Input() info!: string | string[];
  test: string = '';
  ngOnInit() {
    if(typeof this.info === 'string') {
        this.test = this.info;
    }
  }
}

Stackblitz Demo

5
  • But if info is declared as string | string[], should you simply assert it's a string without checking? There must be a reason for this type declaration.
    – mbojko
    Commented Apr 24, 2024 at 7:17
  • @mbojko added type check Commented Apr 24, 2024 at 7:23
  • @NarenMurali thank you for the answer i tried to resolve it but got the same error this code lies in the base class and the child component extends from it. @Directive() export abstract class BaseFilterComponent { @Input() info!: string | string[]; } Commented Apr 24, 2024 at 8:15
  • 1
    @awaisnawaz please replicate the issue in the provided stackblitz and share back! Commented Apr 24, 2024 at 8:20
  • 1
    thank you by doing this error is resolved, using never as datatype and then check typing can resolve the issue. Commented Apr 25, 2024 at 6:45
1

Type checking must be performed to determine whether our variable is of type string or string[]. Once we're certain it's a string with typeof this.info === 'string', we can then pass its value to another variable that accepts ONLY strings.

@NarenMurali already mentioned it in answer, but I think he overthought the example and didn't focus on the essence, so I'm leaving this here:

let message: string | string[] = 'Example'
let result: string = ''

if (typeof message === 'string') {
  // the 'message' variable is a string, so I can pass it as a string
  result = message // it safely receives a string
}
else {
  // the 'message' variable is not a string, so I concatenated the elements of the array into a string
  result = message.split(' ') // message is definitely an array, so split() will work
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.