question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `define` with `strictNullChecks`

See original GitHub issue

I am having trouble using a custom type I have defined. Please see below my use case:

/**
 * The interface I would like to validate.
 */
export interface FsAppAccess {
    code?: string;
    organisation: admin.firestore.DocumentReference;
    organisationHierarchy: admin.firestore.DocumentReference[];
}

/**
 * My custom type for admin.firestore.DocumentReference.
 */
export const organisationDocumentReference = define<admin.firestore.DocumentReference>('organisations-document-reference', (value: admin.firestore.DocumentReference) => {
  return is(value.path, pattern(string(), /^organisations\/[^\/]+$/));
});

/**
 * The complete validation struct.
 */
export const fsAppAccessStruct: Describe<FsAppAccess> = object({
    code: optional(string()),
    organisation: organisationDocumentReference,
    organisationHierarchy: length(array(organisationDocumentReference), 1)
});

admin.firestore.DocumentReference is a class defined in a third party library Firebase. I’m only interested that the object has a specific path, rather than fully validating the whole object, which is unnecessary for my use case. I thought I could accomplish this using the define utility, which had worked perfectly at the time I implemented.

I have since modified my tsconfig to use strictNullChecks, which is now causing a compiler error:

Types of property 'organisation' are incompatible.       
Type 'Struct<DocumentReference<DocumentData>, null>' is not assignable to type 'Describe<DocumentReference<DocumentData>>'

If I modify the line:

organisation: organisationDocumentReference;

to

organisation: organisationDocumentReference | null;

then the problem goes away, but that is definitely not what I want. The property should not be allowed to be null.

Am I holding it wrong?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ianstormtaylorcommented, Mar 24, 2021

@Poliziano can you create a minimal reproduction of this?

0reactions
jcommented, May 12, 2021

I just ran into this issue as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is true , null and undefined have their own distinct types and you'll get a type error if you try to...
Read more >
strictNullChecks - TypeScript Deep Dive - Gitbook
By default null and undefined are assignable to all types in TypeScript e.g. ... optional property, meaning the value of age may or...
Read more >
Strict Null Checks - Advanced TypeScript Masterclass
Back to strictNullChecks . The definition says that when the flag is not enabled, null and undefined values are in the domain of...
Read more >
Typescript strictNullChecks with limited scope - Stack Overflow
Is there a way to declare a directory / file / module / class as abiding by strictNullChecks, even when the overall project...
Read more >
TypeScript "strictNullChecks" - a migration guide
strictNullChecks can, and often will, locate actual bugs in your code. ... you can define a helper function to do it for you:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found