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:

24 questions
30
votes
3 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…
15
votes
3 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
  • 14,618
  • 9
  • 41
  • 96
7
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
  • 73,934
  • 32
  • 166
  • 212
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,486
  • 11
  • 28
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
  • 32,579
  • 35
  • 130
  • 205
4
votes
1 answer

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
  • 558
  • 7
  • 19
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
  • 317
  • 1
  • 3
  • 9
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
  • 6,927
  • 2
  • 53
  • 86
2
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
  • 4,659
  • 4
  • 19
  • 40
1
vote
0 answers

strictNullChecks enabled, calling generic method with optional params gives typing error for subsequent method calls

I have a situation where I have a generic function that takes a type param and then calls another more specific function based on that type. I pass an optional parameter to the generic function that is required for some of the specific functions but…
Stefan SL
  • 11
  • 1
1
vote
0 answers

Why does TypeScript allow null assignment within ternary under strictNullChecks?

I'm new to TypeScript, but I see seemingly inconsistent behavior: I can assign null to an interface as part of a ternary assignment, but I get an error when doing so via if/else flow (please see below). The project is set to enforce…
Stewii
  • 72
  • 2
  • 6
1
vote
1 answer

Getting Typescript strictNullChecks to work with undefined returning vanilla js functions

With idiomatic js returning undefined on error, converted to TS function multiply(foo: number | undefined){ if (typeof foo !== "number"){ return; }; return 5 * foo; } When using multiply in new TS code i get the problem of the…
Vincent J
  • 467
  • 4
  • 16
1
vote
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
  • 44,833
  • 12
  • 104
  • 136
1
vote
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
  • 365
  • 2
  • 10
1
vote
2 answers

Ngrx: Property '[Symbol.observable]' is missing in type 'Store' error

I'm using NGRX v4.1.1 in an Angular v5 app (with "strictNullChecks": true, though it doesn't appear to matter). I'm seeing an error with the store. Given the following: showLists: Observable; constructor(private store: Store)…
John
  • 7,008
  • 3
  • 33
  • 62
1
2