Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(typescript): make note about recommending strict mode when using auto typed schemas #12825

Merged
merged 3 commits into from Dec 27, 2022

Conversation

vkarpov15
Copy link
Collaborator

Re: #12420

Summary

So strange. The following script fails to compile without --strict.

import { Schema, InferSchemaType } from 'mongoose';

const sightSchema = new Schema({
    name: { type: String, required: true },
    location: { type: [Number], required: false } // all ways to define "location" give the same result
    // location: { type: [Number] }
    // location: [Number]
});

type Sight = InferSchemaType<typeof sightSchema>;

// Property 'location' is missing in type '{ name: string; }' but required in type '{ name: string; location: number[]; }'
const sight: Sight = {
    name: 'Red Square',
};
$ ./node_modules/.bin/tsc  gh-12420.ts 
gh-12420.ts:13:7 - error TS2741: Property 'location' is missing in type '{ name: string; }' but required in type '{ location: unknown[]; name?: unknown; }'.

13 const sight: Sight = {
         ~~~~~


Found 1 error in gh-12420.ts:13

From #12420. This PR makes a note in the docs.

Examples

@hasezoey
Copy link
Collaborator

i think the problem more specifically is strictNullChecks, quote:

When strictNullChecks is false, null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.
When strictNullChecks is true, null and undefined have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected.

Typescript: TSConfig Reference

@hasezoey hasezoey added docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request labels Dec 27, 2022
@vkarpov15 vkarpov15 merged commit 2796185 into master Dec 27, 2022
@hasezoey hasezoey deleted the vkarpov15/gh-12420-2 branch December 27, 2022 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants