Argument of type any is not assignable to parameter of type never

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, 2022 in TypeSript by Logan
• 2,140 points
59,258 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, 2022 by Nina
• 3,060 points

Related Questions In TypeSript

0 votes
1 answer

Only refers to a type, but is being used as a value here

To do type checking at runtime with ...READ MORE

answered Jun 8, 2022 in TypeSript by Nina
• 3,060 points
55,699 views
0 votes
1 answer

Typescript: TS7006: Parameter 'xxx' implicitly has an 'any' type

You are using the --noImplicitAny and TypeScript doesn't know ...READ MORE

answered Jun 10, 2022 in TypeSript by Nina
• 3,060 points
23,848 views
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, 2022 in TypeSript by Nina
• 3,060 points
14,422 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, 2022 in TypeSript by Nina
• 3,060 points
3,848 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 ffdfd
• 5,550 points
1,198 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,613 views
0 votes
1 answer

TypeScript TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'

If you want a key/value data structure ...READ MORE

answered Jun 7, 2022 in TypeSript by Nina
• 3,060 points
18,962 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP