Skip to content

Commit 97d62ef

Browse files
authored
Merge pull request #12951 from Automattic/revert-12781-infer-null
Revert "typescript: Allow null for optional document fields"
2 parents 0f2f461 + 1e19236 commit 97d62ef

File tree

4 files changed

+61
-66
lines changed

4 files changed

+61
-66
lines changed

test/types/models.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ function gh12100() {
466466
const TestModel = model('test', schema_with_string_id);
467467
const obj = new TestModel();
468468

469-
expectType<string | null>(obj._id);
469+
expectType<string>(obj._id);
470470
})();
471471

472472
(async function gh12094() {

test/types/queries.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ async function gh12342_manual() {
411411

412412
async function gh12342_auto() {
413413
interface Project {
414-
name?: string | null, stars?: number | null
414+
name?: string, stars?: number
415415
}
416416

417417
const ProjectSchema = new Schema({

test/types/schema.test.ts

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -372,50 +372,50 @@ export function autoTypedSchema() {
372372
}
373373

374374
type TestSchemaType = {
375-
string1?: string | null;
376-
string2?: string | null;
377-
string3?: string | null;
378-
string4?: string | null;
375+
string1?: string;
376+
string2?: string;
377+
string3?: string;
378+
string4?: string;
379379
string5: string;
380-
number1?: number | null;
381-
number2?: number | null;
382-
number3?: number | null;
383-
number4?: number | null;
380+
number1?: number;
381+
number2?: number;
382+
number3?: number;
383+
number4?: number;
384384
number5: number;
385-
date1?: Date | null;
386-
date2?: Date | null;
387-
date3?: Date | null;
388-
date4?: Date | null;
385+
date1?: Date;
386+
date2?: Date;
387+
date3?: Date;
388+
date4?: Date;
389389
date5: Date;
390-
buffer1?: Buffer | null;
391-
buffer2?: Buffer | null;
392-
buffer3?: Buffer | null;
393-
buffer4?: Buffer | null;
394-
boolean1?: boolean | null;
395-
boolean2?: boolean | null;
396-
boolean3?: boolean | null;
397-
boolean4?: boolean | null;
390+
buffer1?: Buffer;
391+
buffer2?: Buffer;
392+
buffer3?: Buffer;
393+
buffer4?: Buffer;
394+
boolean1?: boolean;
395+
boolean2?: boolean;
396+
boolean3?: boolean;
397+
boolean4?: boolean;
398398
boolean5: boolean;
399-
mixed1?: any | null;
400-
mixed2?: any | null;
401-
mixed3?: any | null;
402-
objectId1?: Types.ObjectId | null;
403-
objectId2?: Types.ObjectId | null;
404-
objectId3?: Types.ObjectId | null;
405-
customSchema?: Int8 | null;
406-
map1?: Map<string, string> | null;
407-
map2?: Map<string, number> | null;
399+
mixed1?: any;
400+
mixed2?: any;
401+
mixed3?: any;
402+
objectId1?: Types.ObjectId;
403+
objectId2?: Types.ObjectId;
404+
objectId3?: Types.ObjectId;
405+
customSchema?: Int8;
406+
map1?: Map<string, string>;
407+
map2?: Map<string, number>;
408408
array1: string[];
409409
array2: any[];
410410
array3: any[];
411411
array4: any[];
412412
array5: any[];
413413
array6: string[];
414-
array7?: string[] | null;
415-
array8?: string[] | null;
416-
decimal1?: Types.Decimal128 | null;
417-
decimal2?: Types.Decimal128 | null;
418-
decimal3?: Types.Decimal128 | null;
414+
array7?: string[];
415+
array8?: string[];
416+
decimal1?: Types.Decimal128;
417+
decimal2?: Types.Decimal128;
418+
decimal3?: Types.Decimal128;
419419
};
420420

421421
const TestSchema = new Schema({
@@ -552,17 +552,17 @@ export function autoTypedSchema() {
552552
export type AutoTypedSchemaType = {
553553
schema: {
554554
userName: string;
555-
description?: string | null;
555+
description?: string;
556556
nested?: {
557557
age: number;
558-
hobby?: string | null
559-
} | null,
560-
favoritDrink?: 'Tea' | 'Coffee' | null,
558+
hobby?: string
559+
},
560+
favoritDrink?: 'Tea' | 'Coffee',
561561
favoritColorMode: 'dark' | 'light'
562-
friendID?: Types.ObjectId | null;
562+
friendID?: Types.ObjectId;
563563
nestedArray: Types.DocumentArray<{
564564
date: Date;
565-
messages?: number | null;
565+
messages?: number;
566566
}>
567567
}
568568
, statics: {
@@ -639,7 +639,7 @@ function gh12003() {
639639

640640
expectType<'type'>({} as ObtainSchemaGeneric<typeof BaseSchema, 'TSchemaOptions'>['typeKey']);
641641

642-
expectType<{ name?: string | null }>({} as BaseSchemaType);
642+
expectType<{ name?: string }>({} as BaseSchemaType);
643643
}
644644

645645
function gh11987() {
@@ -675,7 +675,7 @@ function gh12030() {
675675
}
676676
]>;
677677
expectType<{
678-
username?: string | null
678+
username?: string
679679
}[]>({} as A);
680680

681681
type B = ObtainDocumentType<{
@@ -687,13 +687,13 @@ function gh12030() {
687687
}>;
688688
expectType<{
689689
users: {
690-
username?: string | null
690+
username?: string
691691
}[];
692692
}>({} as B);
693693

694694
expectType<{
695695
users: {
696-
username?: string | null
696+
username?: string
697697
}[];
698698
}>({} as InferSchemaType<typeof Schema1>);
699699

@@ -715,7 +715,7 @@ function gh12030() {
715715
expectType<{
716716
users: Types.DocumentArray<{
717717
credit: number;
718-
username?: string | null;
718+
username?: string;
719719
}>;
720720
}>({} as InferSchemaType<typeof Schema3>);
721721

@@ -724,7 +724,7 @@ function gh12030() {
724724
data: { type: { role: String }, default: {} }
725725
});
726726

727-
expectType<{ data: { role?: string | null } }>({} as InferSchemaType<typeof Schema4>);
727+
expectType<{ data: { role?: string } }>({} as InferSchemaType<typeof Schema4>);
728728

729729
const Schema5 = new Schema({
730730
data: { type: { role: Object }, default: {} }
@@ -749,7 +749,7 @@ function gh12030() {
749749
track?: {
750750
backupCount: number;
751751
count: number;
752-
} | null;
752+
};
753753
}>({} as InferSchemaType<typeof Schema6>);
754754

755755
}
@@ -826,7 +826,7 @@ function gh12450() {
826826
});
827827

828828
expectType<{
829-
user?: Types.ObjectId | null;
829+
user?: Types.ObjectId;
830830
}>({} as InferSchemaType<typeof ObjectIdSchema>);
831831

832832
const Schema2 = new Schema({
@@ -841,14 +841,14 @@ function gh12450() {
841841
decimalValue: { type: Schema.Types.Decimal128 }
842842
});
843843

844-
expectType<{ createdAt: Date, decimalValue?: Types.Decimal128 | null }>({} as InferSchemaType<typeof Schema3>);
844+
expectType<{ createdAt: Date, decimalValue?: Types.Decimal128 }>({} as InferSchemaType<typeof Schema3>);
845845

846846
const Schema4 = new Schema({
847847
createdAt: { type: Date },
848848
decimalValue: { type: Schema.Types.Decimal128 }
849849
});
850850

851-
expectType<{ createdAt?: Date | null, decimalValue?: Types.Decimal128 | null }>({} as InferSchemaType<typeof Schema4>);
851+
expectType<{ createdAt?: Date, decimalValue?: Types.Decimal128 }>({} as InferSchemaType<typeof Schema4>);
852852
}
853853

854854
function gh12242() {
@@ -872,7 +872,7 @@ function testInferTimestamps() {
872872
// an error "Parameter type { createdAt: Date; updatedAt: Date; name?: string | undefined; }
873873
// is not identical to argument type { createdAt: NativeDate; updatedAt: NativeDate; } &
874874
// { name?: string | undefined; }"
875-
expectType<{ createdAt: Date, updatedAt: Date } & { name?: string | null }>({} as WithTimestamps);
875+
expectType<{ createdAt: Date, updatedAt: Date } & { name?: string }>({} as WithTimestamps);
876876

877877
const schema2 = new Schema({
878878
name: String
@@ -898,25 +898,25 @@ function gh12431() {
898898
});
899899

900900
type Example = InferSchemaType<typeof testSchema>;
901-
expectType<{ testDate?: Date | null, testDecimal?: Types.Decimal128 | null }>({} as Example);
901+
expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example);
902902
}
903903

904904
async function gh12593() {
905905
const testSchema = new Schema({ x: { type: Schema.Types.UUID } });
906906

907907
type Example = InferSchemaType<typeof testSchema>;
908-
expectType<{ x?: Buffer | null }>({} as Example);
908+
expectType<{ x?: Buffer }>({} as Example);
909909

910910
const Test = model('Test', testSchema);
911911

912912
const doc = await Test.findOne({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' }).orFail();
913-
expectType<Buffer | undefined | null>(doc.x);
913+
expectType<Buffer | undefined>(doc.x);
914914

915915
const doc2 = new Test({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' });
916-
expectType<Buffer | undefined | null>(doc2.x);
916+
expectType<Buffer | undefined>(doc2.x);
917917

918918
const doc3 = await Test.findOne({}).orFail().lean();
919-
expectType<Buffer | undefined | null>(doc3.x);
919+
expectType<Buffer | undefined>(doc3.x);
920920

921921
const arrSchema = new Schema({ arr: [{ type: Schema.Types.UUID }] });
922922

@@ -982,7 +982,7 @@ function gh12611() {
982982
expectType<{
983983
description: string;
984984
skills: Types.ObjectId[];
985-
anotherField?: string | null;
985+
anotherField?: string;
986986
}>({} as Props);
987987
}
988988

types/inferschematype.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ declare module 'mongoose' {
2626
*/
2727
type ObtainDocumentType<DocDefinition, EnforcedDocType = any, TSchemaOptions extends Record<any, any> = DefaultSchemaOptions> =
2828
IsItRecordAndNotAny<EnforcedDocType> extends true ? EnforcedDocType : {
29-
[K in keyof (
30-
RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
31-
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>
32-
)]:
33-
IsPathRequired<DocDefinition[K], TSchemaOptions['typeKey']> extends true ?
34-
ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>:
35-
ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']> | null;
29+
[K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
30+
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)]: ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
3631
};
3732

3833
/**

0 commit comments

Comments
 (0)