@@ -38,7 +38,7 @@ import PopulateUtil from './utils/populate';
3838
3939import { inMemoryPublisher } from './events/in-memory' ;
4040
41- const defaultOptions : ServiceOptions < IDocument > = {
41+ const defaultOptions : ServiceOptions < IDocument , ReadonlyArray < keyof IDocument > > = {
4242 skipDeletedOnDocs : true ,
4343 publishEvents : true ,
4444 outbox : false ,
@@ -50,14 +50,14 @@ const defaultOptions: ServiceOptions<IDocument> = {
5050
5151const isDev = process . env . NODE_ENV === 'development' ;
5252
53- class Service < T extends IDocument > {
53+ class Service < T extends IDocument , PrivateFields extends ReadonlyArray < keyof T > = [ ] > {
5454 private client ?: MongoClient ;
5555
5656 private collection : Collection < T > | null ;
5757
5858 private _collectionName : string ;
5959
60- private options : ServiceOptions < T > ;
60+ private options : ServiceOptions < T , PrivateFields > ;
6161
6262 private db ;
6363
@@ -68,14 +68,14 @@ class Service<T extends IDocument> {
6868 constructor (
6969 collectionName : string ,
7070 db : IDatabase ,
71- options : ServiceOptions < T > = { } ,
71+ options : ServiceOptions < T , PrivateFields > = { } ,
7272 ) {
7373 this . _collectionName = collectionName ;
7474 this . db = db ;
7575 this . options = {
7676 ...defaultOptions ,
7777 ...options ,
78- } ;
78+ } as ServiceOptions < T , PrivateFields > ;
7979 this . waitForConnection = db . waitForConnection ;
8080
8181 if ( this . options . outbox ) {
@@ -990,9 +990,13 @@ class Service<T extends IDocument> {
990990 }
991991 } ;
992992
993- getPublic = < U extends T = T > ( doc : U | null ) : Partial < U > | null => {
994- return omitPrivateFields < U > ( doc , this . options . privateFields || [ ] ) ;
995- } ;
993+ getPublic ( doc : null ) : null ;
994+
995+ getPublic ( doc : T ) : Omit < T , PrivateFields [ number ] > ;
996+
997+ getPublic ( doc : T | null ) : Omit < T , PrivateFields [ number ] > | null {
998+ return omitPrivateFields < T , PrivateFields [ number ] > ( doc , this . options . privateFields || [ ] ) ;
999+ }
9961000}
9971001
9981002export default Service ;
0 commit comments