简体   繁体   English

TS2322:输入&#39;Promise <Hero | undefined> &#39;不可分配给&#39;Promise <Hero> “

[英]TS2322: Type 'Promise<Hero | undefined>' is not assignable to type 'Promise<Hero>'

I'm learning angular4 from angular tutorial . 我正在从角度教程中学习angular4。 Below is a function to get a hero from service's function: 以下是从服务功能中获得英雄的功能:

@Injectable()
export class HeroService {
    getHeroes(): Promise<Hero[]> {

        return new Promise(resolve => {
            // Simulate server latency with 2 second delay
            setTimeout(() => resolve(HEROES), 2000);
        });
    }

    getHero(id: number): Promise<Hero> {

        if (id < 0) {
            throw new Error('Hero is not found.');
        }
        return this.getHeroes()
            .then(heroes => heroes.find(hero => hero.id === id));
    }
}

On execution it throws error: 执行时会引发错误:

TS2322: Type 'Promise<Hero | undefined>' is not assignable to type 'Promise<Hero>'.
Type 'Hero | undefined' is not assignable to type 'Hero'.
Type 'undefined' is not assignable to type 'Hero'.

Anybody else also faced this issue ? 还有其他人遇到这个问题吗? Please help. 请帮忙。 Thank you. 谢谢。

@Component({
    selector: 'hero-detail',
    templateUrl: './hero-detail.component.html'
})
export class HeroDetailComponent implements OnInit {
    @Input() hero: Hero;

    constructor(private heroService: HeroService, private route: ActivatedRoute, private location: Location) { }

    ngOnInit(): void {
        this.route.paramMap
            .switchMap((params: ParamMap) => this.heroService.getHero(+(params.get('id') || -1)))
            .subscribe(hero => this.hero = hero);
    }

    goBack(): void {
        this.location.back();
    }
}

The reason typescript throws this error is because Array.find will return undefined when all elements don't matched the condition hero.id === id . 类型Array.find引发此错误的原因是,当所有元素都不符合条件 hero.id === id时, Array.find返回undefined

Refer doc about Array.find : 请参考有关Array.find 文档

The find() method returns the value of the first element in the array that satisfies the provided testing function. find()方法返回满足提供的测试功能的数组中第一个元素的值。 Otherwise undefined is returned. 否则返回undefined。


To avoid the error, you should change your function's return type to Promise<Hero | undefined> 为避免该错误,应将函数的返回类型更改为Promise<Hero | undefined> Promise<Hero | undefined> . Promise<Hero | undefined>

getHero(id: number): Promise<Hero | undefined> {    // <----mention here for return type
  return this.getHeroes()
             .then(heroes => heroes.find(hero => hero.id === id));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度错误 TS2322:&#39;Promise<Dish | undefined> &#39; 不可分配给类型 &#39;Promise<Dish> &#39; - Angular error TS2322:'Promise<Dish | undefined>' is not assignable to type 'Promise<Dish>' angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string TS2322:类型&#39;(数据:TicketFullDTO)=&gt; 承诺<void> &#39; 不能分配给类型 &#39;FormEventHandler<HTMLFormElement> &#39; - TS2322: Type '(data: TicketFullDTO) => Promise<void>' is not assignable to type 'FormEventHandler<HTMLFormElement>' Typescript - React 错误 TS2322:类型 'AppState | undefined' 不能分配给类型 'ICards | 不明确的' - Typescript - React Error TS2322: Type 'AppState | undefined' is not assignable to type 'ICards | undefined' 类型“IntersectionObserver”不可分配给类型“null”。 TS2322 [React,intersectionObserver] - Type 'IntersectionObserver' is not assignable to type 'null'. TS2322 [React, intersectionObserver] 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 “Firebase”类型不能分配给“null”类型。 TS2322 - Type 'Firebase' is not assignable to type 'null'. TS2322 TS2322:类型“计数”不可分配给类型“ReactNode” - TS2322: Type 'Count' is not assignable to type 'ReactNode' TS2322:类型“BillingSummaryDTO[]”不可分配给类型“从不” - TS2322: Type 'BillingSummaryDTO[]' is not assignable to type 'never' TypeScript TS2322:类型'typeof Foo'不能分配给'IFoo'类型 - TypeScript TS2322: Type 'typeof Foo' is not assignable to type 'IFoo'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM