Skip to content

Array indexing and logical OR with null behave differently depending on the array type #34998

@vkrol

Description

@vkrol

TypeScript 3.7.2
Playground link

Compiler Options:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Input:

function foo(items: Number[]) {
  const firstElement = items[0];
  (firstElement || null).toString();
}

function bar(items: number[]) {
  const firstElement = items[0];
  (firstElement || null).toString();
}

foo([]);
bar([]);

Output:

"use strict";
function foo(items) {
    const firstElement = items[0];
    (firstElement || null).toString();
}
function bar(items) {
    const firstElement = items[0];
    (firstElement || null).toString();
}
foo([]);
bar([]);

Actual behavior:
Compilation error in the second case only.

Expected behavior:
Compilation errors in both cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions