Aaron Ayalew
Aaron Ayalew

Reputation: 123

Non null assertion operator still raising compile time error

I'm using the ! operator to tell the compiler to ignore the possible undefined value but it still seems to raise an error.

let rooms :Room[];
            rooms = [];
            for (let id of roomIds) {
                let room: Room = await getRepository(Room).findOne({where: {id: id}})!;
                if(room) rooms.push(room);
            }

and the error

src/resolvers/BookingResolver.ts:65:21 - error TS2322: Type 'Room | undefined' is not assignable to type 'Room'.
  Type 'undefined' is not assignable to type 'Room'.

65                 let room: Room = await getRepository(Room).findOne({where: {id: id}})!;
                       ~~~~

    at createTSError (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:513:12)
    at reportTSError (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:517:19)
    at getOutput (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:752:36)
    at Object.compile (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:968:32)
    at Module.m._compile (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:1056:42)
    at Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Object.require.extensions.<computed> [as .ts] (/home/tes/work/adey/source/AdeyBackEnd/server/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
[nodemon] app crashed - waiting for file changes before starting.

Upvotes: 1

Views: 437

Answers (1)

Dai
Dai

Reputation: 155438

I assume findOne returns a Promise<Room | undefined>, in which case you need to apply the ! operator to the result of await, but right now you're applying ! before the await.

So change your code to something like this:

const rooms: Room[] = [];
for( let id of roomIds ) {
    const room: Room = ( await getRepository(Room).findOne( { where: { id: id } } ) )!;
    rooms.push( room );
}

...though personally I'd write it like this:

const rooms: Room[] = [];
for( let id of roomIds ) {
    const room: Room | undefined = await getRepository(Room).findOne( { where: { id: id } } );
    rooms.push( room! );
}

...or better yet, use a single repository object and with parallel requests so the program will run much faster overall:

const roomsRepo    = getRepository(Room);
const roomsResults = await Promise.all( roomIds.map( roomId => roomsRepo.findOne( { where: { id: roomId } } ) ) );
const rooms        = roomsResults.filter( r => r ); // Filter out `undefined` values.

Upvotes: 2

Related Questions