Typescript: for-loop: Type 'number' is not assignable to type 'never'.

Created on 6 Sep 2016  Â·  5Comments  Â·  Source: microsoft/TypeScript

TypeScript Version: 2.0.2

Code

// example.ts
function stoBA(d: string): number[] {
  var b = [];
  for (var c = 0; c < d.length; c++) {
    b[c] = d.charCodeAt(c)
  }
  return b
}

Expected behavior:
No error.

Actual behavior:

example.ts:5:4
Type 'number' is not assignable to type 'never'.
Question Working as Intended

Most helpful comment

You need to type annotate b to an array of number. An empty array is of type never without the type annotations.

You could write:

var b = [] as number[];

All 5 comments

You need to type annotate b to an array of number. An empty array is of type never without the type annotations.

You could write:

var b = [] as number[];

or:

var b: number[] = [];
/* or */
var b = <number[]> [];

Thanks, I missed that type annotation. Nevertheless, I find that error message confusing. I only get it if strictNullChecks are enabled. What does this option have to do with this error? b is of type Array which is equivalent to any[]. In the assignment

b[c] = d.charCodeAt(c)

b[c] is of type any and d.charCodeAt(c) of type number. That assignment should be ok. Why does the compiler complain that type number is not assignable to never. b[c] is of type any, isn't it? What causes never?

I only get it if strictNullChecks are enabled. What does this option have to do with this error?

While strictNullChecks implies that it is just checking for usage of variables that might be undefined or null it really turns the compiler into a very pessimistic mode, where when there are no contextual way of inferring the type, it will choose the narrowest type, instead of the widest type, making it so you as a developer are required to provide more information to ensure your code is working as designed.

So here:

var b = []; // What is supposed to be in this array? So never[]
var c = [ 'foo' ]; // I can guess, so string[]
var d = [ 'foo', 1 ]; // I can guess so (string | number)[]

It is because an empty array is of type never, which any cannot fit in.

On Tue, Sep 6, 2016, 8:35 PM Michael Maier [email protected] wrote:

Thanks, I missed that type annotation. Nevertheless, I find that error
message confusing. I only get it if strictNullChecks are enabled. What
does this option have to do with this error? b is of type Array which is
equivalent to any[]. In the assignment

b[c] = d.charCodeAt(c)

b[c] is of type any and d.charCodeAt(c) of type number. That assignment
should be ok. Why does the compiler complain that type number is not
assignable to never. b[c] is of type any, isn't it? What causes never?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Microsoft/TypeScript/issues/10721#issuecomment-245037952,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABXSZvcjrE9MhUi8wnZC3TPCDq7Wx_tmks5qnawpgaJpZM4J11GR
.

Med venlig hilsen / Best regards
Brian K. Christensen

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yortus picture yortus  Â·  157Comments

rwyborn picture rwyborn  Â·  210Comments

rbuckton picture rbuckton  Â·  139Comments

fdecampredon picture fdecampredon  Â·  358Comments

Gaelan picture Gaelan  Â·  231Comments