最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript Object is possibly undefined after type guard error TS2532 - Stack Overflow

programmeradmin5浏览0评论

I'm using Typescript 2.3.4, Node.JS 8.0.0, and the Feathers framework (version 2.1.1). I am making an express route that uses a service, and when I try to use the service after grabbing the singleton instance on the feathers app, Typescript is throwing an error TS2532: Object is possibly 'undefined' error, even after an explicit type guard.

routes.ts

import feathers = require('feathers');

export default function(this: feathers.Application) {
  const app = this;

  app.post(
    '/donuts',
    async (req, res) => {
      const someService = app.service<any>('predefined-service');

      if(typeof someService === "undefined" || !authService) {
        res.redirect('/fail');
        return;
      }

      try {
        let data = someService.create({test: 'hello'});
        console.log(data);
        res.redirect('/success');
      } catch(err) {
        res.redirect('/fail');
      }
    }
}

I've also tried writing someService!.create... but that didn't work either.

I'm using Typescript 2.3.4, Node.JS 8.0.0, and the Feathers framework (version 2.1.1). I am making an express route that uses a service, and when I try to use the service after grabbing the singleton instance on the feathers app, Typescript is throwing an error TS2532: Object is possibly 'undefined' error, even after an explicit type guard.

routes.ts

import feathers = require('feathers');

export default function(this: feathers.Application) {
  const app = this;

  app.post(
    '/donuts',
    async (req, res) => {
      const someService = app.service<any>('predefined-service');

      if(typeof someService === "undefined" || !authService) {
        res.redirect('/fail');
        return;
      }

      try {
        let data = someService.create({test: 'hello'});
        console.log(data);
        res.redirect('/success');
      } catch(err) {
        res.redirect('/fail');
      }
    }
}

I've also tried writing someService!.create... but that didn't work either.

Share Improve this question edited Oct 21, 2019 at 8:36 Victor 3,8284 gold badges39 silver badges45 bronze badges asked Jun 9, 2017 at 19:35 Sean NewellSean Newell 1,1132 gold badges10 silver badges26 bronze badges 2
  • What is the type of someService? What line does the error occur on? – dbandstra Commented Jun 9, 2017 at 20:50
  • Error is on let data = someService.create({test: 'hello'}); line, and someService is defined as a Service<any> type as per the app.service<any>('predefined-service') call (that type es from feathers). Feathers services work by first registering them. – Sean Newell Commented Jun 9, 2017 at 20:53
Add a ment  | 

1 Answer 1

Reset to default 15

From the Feathers typings:

interface Service<T> extends events.EventEmitter {

  create?(data: T | T[], params?: Params, callback?: any): Promise<T | T[]>;

The create method itself is optional (for whatever reason). If you're sure the method exists you could put the ! operator after, like this:

let data = someService.create!({test: 'hello'});
发布评论

评论列表(0)

  1. 暂无评论