Typescript discriminated union types with Observable.of
我正在尝试将Typescript 2.0的已区分联合类型与RxJS一起使用,但是我收到一个错误,即我返回的对象不是联合类型的类型之一。
这是我的类型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | interface Square { kind:"square"; width: number; } interface Circle { kind:"circle"; radius: number; } interface Center { kind:"center"; } type Shape = Square | Circle | Center; |
我只返回不使用
1 2 3 4 5 6 7 8 9 | function shapeFactory(width: number): Shape { if (width > 5) { return {kind:"circle", radius: width}; } else if (width < 2) { return {kind:"square", width: 3}; } return {kind:"center"}; } |
当我尝试像这样返回
1 2 3 4 5 6 7 | function shapeFactoryAsync(width: number): Observable<Shape> { if (width > 5) { return Observable.of({kind:"circle", radius: width}); } else { return Observable.of({kind:"center"}); } } |
我遇到编译错误:
1 2 3 4 5 | Type 'Observable<{ kind: string; radius: number; }>' is not assignable to type 'Observable<Shape>'. Type '{ kind: string; radius: number; }' is not assignable to type 'Shape'. Type '{ kind: string; radius: number; }' is not assignable to type 'Center'. Types of property 'kind' are incompatible. Type 'string' is not assignable to type '"center"'. |
我希望我的第一个返回将是
如果我明确分配了对象并为该分配指定了类似的类型,则可以修复该问题:
1 2 | let circle: Circle = {kind:"circle", radius: width}; return Observable.of(circle); |
虽然这似乎应该是不必要的强制转换。
我只是在做这件事完全错误,还是为了确定
使用
您可以通过在调用泛型
1 2 3 4 5 6 7 | function shapeFactoryAsync(width: number): Observable<Shape> { if (width > 5) { return Observable.of<Shape>({ kind:"circle", radius: width }); } else { return Observable.of<Shape>({ kind:"center" }); } } |
使用指定的类型变量,TypeScript不再需要推断类型。