不兼容的类型错误,但字段推断为兼容

时间:2018-07-18 20:25:59

标签: typescript

在case语句中,FUNCTION func_RowCount ( in_FromDate IN DATE, in_ThruDate IN DATE ) RETURN NUMBER AS lvnCount NUMBER; lvsSqlStr VARCHAR2(2000); BEGIN lvsSqlStr := 'SELECT COUNT(*) FROM SomeTable WHERE StartDate > :in_FromDate AND StartDate < :in_ThruDate'; EXECUTE IMMEDIATE lvsSqlStr INTO lvnCount USING in_FromDate, in_ThruDate; RETURN lvnCount; END func_RowCount; 的类型缩小为obj.value,但是仍然存在错误,无法在下一行将value字段转换为E.Foo

E.Foo

我的问题是,为什么不使用switch语句缩小enum E { Foo, Bar } interface Base { value: E, x: number, y: string, } type Foo = Base & { value: E.Foo }; type Bar = Base & { value: E.Bar }; function test(obj: Base) { switch (obj.value) { case E.Foo: const value: E.Foo = obj.value; func(obj); // Error, even though the previous line is accepted func({ ...obj, value: obj.value }); // Accepted by the compiler, but it's ugly and copies obj } } function func(obj: Foo) { console.log(obj.x); console.log(obj.y); } 的类型。在switch语句之外,obj的类型为obj。在case语句中,{ value: E, ... }缩小为obj.value,这意味着E.Foo的类型只能是obj{ value: E.Foo, ... }证明该字段已缩小,但是当我将其传递给const value: E.Foo = obj.value;时,仍然出现错误消息。

链接:Playground

0 个答案:

没有答案