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
1
vote
0 answers

Why typescript with strictNullChecks=true doesn't complain when accessing the property of an object?

Probably there is something I don't understand. In this code I expect typescript to emit a warning for the line page.name = 'foo'; because page can be null? export interface Page { id: string; name: string; } const pages: { [id: string]: Page }…
Christophe
  • 11
  • 1
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
  • 761
  • 1
  • 5
  • 22
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
  • 9,249
  • 5
  • 44
  • 76
1
vote
2 answers

How to tell Typescript that *in this instance* a function's return type is never null

I'm trying to get typescript strictNullChecks working in an Angular 5 project. I have a form: this.signinForm = this.fb.group({ emailAddress: ['', NGValidators.isEmail()], password: ['', Validators.required], rememberMe: false, }); I can get…
John
  • 9,249
  • 5
  • 44
  • 76
0
votes
1 answer

Why does "any" type disable typechecking prematurely?

Why does using the any type disable strict null checks in the below code? Is this a bug? type SomeType = any; interface Optional { some?: { key: SomeType; }; } interface NotOptional { key: SomeType; } const opt: Optional = {}; const…
NoBullsh1t
  • 483
  • 4
  • 14
0
votes
0 answers

Because of `strictNullChecks` return type of function is ` | undefined` although it will never be undefined

The function is as follows: const getProp = < TObj extends object, TKey extends keyof TObj >(props: TObj, key: TKey, def: TObj[TKey]): TObj[TKey] => { if (props[key] == null) return def return props[key] } When used as follows: const fluid…
Tim Baas
  • 6,035
  • 5
  • 45
  • 72
0
votes
1 answer

Type alias from property that might be null with strictNullChecks

I am using graphql-codegen/typescript-apollo-angular to generate typescript code can be useed in our Angular application. We decided to enable strictNullChecks the only issue is that we use the following pattern to specify the types of inner nodes…
Jonas Osburg
  • 1,733
  • 1
  • 14
  • 17
0
votes
2 answers

Typescript strictNullChecks does not narrow type

I have a simple case when strictNullChecks does not narrow type even though I explicitly check for undefined. interface WideType { one: { two: number } | undefined; } interface NarrowType { one: { two: number }; } const example = (value:…
Yan
  • 920
  • 1
  • 7
  • 13
0
votes
1 answer

TypeScript: Can I check if a type contains undefined even when strictNullChecks is false?

We had an Issue in redux-starter-kit where a user of the library had strictNullChecks disabled and one of our type tests was short-cicuiting, returning types for a different case. This test should return the True-Parameter or the False-Parameter…
phry
  • 35,762
  • 5
  • 67
  • 81
0
votes
0 answers

Typescript Strict Null Checks on Objects With optional keys

I am introducing the strictNullchecks compiler option in our Angular project and have come across this issue. error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. replaceTokens(menuItemList:…
James
  • 2,516
  • 2
  • 19
  • 31
0
votes
1 answer

Typescript strictNullChecks error when not possible

I am attempting to turn on the strictNullChecks setting for my project, but have a rather curious error in the following snippet of code: toasters.forEach((toster: ToasterObject) => { if (toaster.brandName) { //This line works just fine …
dmoore1181
  • 1,793
  • 1
  • 25
  • 57
-1
votes
1 answer

Is there a good way of telling typescript all array elements exist in a Map?

Let's say I'm creating a Map from an array of objects with ids as keys, then accessing the map from a different array that has the same ids: const arr1 = [ {id: 'a', firstName: 'Jeff'}, {id: 'b', firstName: 'John'}, {id: 'c', firstName:…
-1
votes
1 answer

Can I solve this one without strictNullChecks: false?

I wonder how can I solve this without setting strictNullchecks to false. if (someArray.find(element => element.id === x.id) { return someArray.find(element => element.id === x.id).message } or const x = someArray.find(element => element.id ===…
Noah
  • 3
  • 2
1
2