Using `define` with `strictNullChecks`
See original GitHub issueI am having trouble using a custom type I have define
d. 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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Poliziano can you create a minimal reproduction of this?
I just ran into this issue as well.