Questions tagged [strictnullchecks]

--strictNullChecks (cli) or strictNullChecks: true (tsconfig.json) is a compiler flag for .

When strictNullChecks is false, null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime. Setting strictNullChecks to true will raise an error that you have not made a guarantee that the value exists before trying to use it.

Resources:

30 questions
79
votes
7 answers

Why does TypeScript infer the 'never' type when reducing an Array with concat?

Code speaks better than language, so: ['a', 'b', 'c'].reduce((accumulator, value) => accumulator.concat(value), []); The code is very silly and returns a copied Array... TS complains on concat's argument: TS2345: Argument of type 'string' is not…
millsp
  • 1,259
  • 1
  • 10
  • 23
52
votes
4 answers

Typescript and filter Boolean

Consider following code with strictNullChecks turned on: var a: (number | null)[] = [0, 1, 2, 3, null, 4, 5, 6]; var b: { value: number; }[] = a.map(x => x != null && { value: x }).filter(Boolean); It fails to compile due to: Type '(false | {…
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
15
votes
2 answers

TypeScript with strict null checking - what about array access?

In TypeScript, if strict null checking is enabled, I would expect the compiler to prevent me from assigning null or undefined values to a variable unless it admits null. However, array access seems to allow circumventing this check. Example: let a:…
sleske
  • 81,358
  • 34
  • 189
  • 227
8
votes
2 answers

Typescript: Usage of Map<> with strictNullChecks

Given the following simple class: class Observer { private subscribers: Map void)>> = new Map(); public subscribe(event: string, callback: (data: any) => void) { if…
tklepzig
  • 598
  • 9
  • 19
5
votes
1 answer

createStore with strict typescript

import { createStore } from "redux"; import * as storage from "redux-storage"; const reducer = storage.reducer((state, action) => ({})); const store = createStore(reducer, {}); When using the above code with strict enabled for typescript, I am…
ed'
  • 1,815
  • 16
  • 30
5
votes
1 answer

How to implement strictNullChecks in angular 4

I have tried to implement strictNullChecks in angular 4 application. I have just added "strictNullChecks": true in tsconfig.json When I run the application ng serve I got this below error. ERROR in [at-loader]…
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
4
votes
1 answer

Can I disable distinguishing between null and undefined in TypeScript strict null-checking mode?

I'm currently converting a large TypeScript codebase to strict null-checks. The codebase has many types with optional members: interface MyInterface { member1?: number; member2?: string; } Furthermore, it uses a type Nullable = T | null, and…
Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148
4
votes
1 answer

What should I return when object is not found in TypeScript using strict mode?

Here's the snippet of code in typescript with strictNullChecks: true getItem(key: string): T { let index: number = this.contains(key); // returns -1 if not found if (index === -1) { return null; // error null is not assignable to…
Martin Čuka
  • 16,134
  • 4
  • 23
  • 49
4
votes
2 answers

Typescript with --strictNullCheck - check not null in separate method

I am having fun with the compiler --strictNullCheck option I have this method: I need to check if the headers are not null before i can use them. That's great Now I would like to move the checking operation to the separate method like this: But…
Dawid Dyrcz
  • 355
  • 1
  • 4
  • 10
2
votes
1 answer

Use 'strictNullChecks' annotation on one function/method

I have this method: remove(node: SortedQueueNode) : SortedQueueNode{ // ... return node.parent; } there are multiple return statements in the function body, and I want to avoid null/undefined return statements. Is there an…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
2
votes
1 answer

Compare undefined with numbers with strictNullChecks enabled

strictNullChecks is a great option, which can help developers avoid many potential runtime errors. So I recently started to refactor my code with this option enabled. But then I came across a small but slightly annoying scenario: if (array?.length >…
0x269
  • 688
  • 8
  • 20
2
votes
1 answer

Type narrowing: checking variable object key existence when noUncheckedIndexedAccess is true

I have "noUncheckedIndexedAccess": true in my tsconfig.json. The whole point of the switch is to force the check of existence of the item at an index before accessing it. I struggle to do the check for object with a variable key: Say I have the…
jhrdka
  • 45
  • 4
2
votes
1 answer

Allow nullable operands in less- or greater-than comparisons in TypeScript

In JavaScript, null operands in a relational expression are treated as 0: function f() { return /* either a number or null */; } let b = f() < 0; // false if f() returns null But in TypeScript, if I provide a type annotation for f and turn on…
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
2
votes
2 answers

TypeScript: check that the required properties of an argument are defined before passing it to a function call

This doesn't compile (playground): function myFunction(params: { a: Date, b?: Date }) { if (params.b) { myFunctionInternal(params); // ERROR! } } function myFunctionInternal(params: { a: Date, b: Date }) {} Is there…
thorn0
  • 9,362
  • 3
  • 68
  • 96
2
votes
2 answers

Typescript Why type guard does not work as expected for array of objects of type null-able union(strictNullChecks enabled)

type Field = {test: {more: number} | null} let fields: Field[] = [{test: {more: 55}}] Transpiler throws error regardless of the type guard: if (fields[0].test) { fields[0].test.more = 55 // object is possibly null } Here no error: function…
Marta Brzeszczyk
  • 425
  • 3
  • 13
1
2