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
• 1,580 points
33 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
• 2,900 points

Related Questions In TypeSript

0 votes
1 answer
0 votes
1 answer

TypeScript Object assign gives me an error property assign does not exist on type ObjectConstructor

For TypeScript 2.1 and higher, you can ...READ MORE

answered May 31 in TypeSript by Nina
• 2,900 points
28 views
0 votes
1 answer

Is it possible to extend types in Typescript?

you can intersect types: type TypeA = { ...READ MORE

answered Jun 7 in TypeSript by Nina
• 2,900 points
18 views
0 votes
1 answer

What is record in TypeScript

A Record<K, T> is an object type whose property ...READ MORE

answered Jun 7 in TypeSript by Nina
• 2,900 points
22 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
• 2,900 points
57 views
0 votes
1 answer
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
430 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
• 2,900 points
8 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