What is not assignable to parameter of type never error in TypeScript

0 votes

This is my code:

const foo = (foo: string) => {
  const result = []
  result.push(foo)
}

I get the following TS error:

[ts] Argument of type 'string' is not assignable to parameter of type 'never'.

What exactly am I doing wrong?

May 31 in TypeSript by Logan
• 2,140 points
868 views

1 answer to this question.

0 votes

All you have to do is define your result as a string array, like this:

const result : string[] = [];

Without defining the array type, by default, it will be never. So when you tried to add a string to it, it was a type mismatch, and so it threw the error you saw.

answered May 31 by Nina
• 3,020 points

Related Questions In TypeSript

0 votes
1 answer
0 votes
0 answers

What is a type in Typescript for the Component class in Angular 2+?

I have a small problem, but big ...READ MORE

Jul 5 in TypeSript by Logan
• 2,140 points
13 views
0 votes
1 answer
0 votes
1 answer

TypeScript - ',' expected.ts(1005)

You can't put statements in JSX curly braces, only expressions. You ...READ MORE

answered Jun 9 in TypeSript by Nina
• 3,020 points
618 views
0 votes
1 answer

TypeScript and React Native: Are the type definitions for RN styles wrong?

You can test some of ways for ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
133 views
0 votes
1 answer

error on aggregate and non aggregate values

Create a calculated field that outputs a ...READ MORE

answered Apr 17, 2018 in Tableau by sdfg
• 5,550 points
444 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
13 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP