- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,类型定义:
// type definitions
class GenericDto {
public ids: string[] = [];
public dateFrom: string | null = null;
public dateTo: string | null = null;
}
class FleetInformationDto extends GenericDto { }
class VehicleInformationDto extends GenericDto { }
enum ReportQueueAction {
GENERATE
}
enum ReportQueueType {
VEHICLE_INFORMATION,
FLEET_INFORMATION
}
type ReportQueue = {
action: ReportQueueAction;
type: ReportQueueType.FLEET_INFORMATION;
dto: FleetInformationDto
} | {
action: ReportQueueAction,
type: ReportQueueType.VEHICLE_INFORMATION,
dto: VehicleInformationDto;
}
和实现:
// implementation
const dto: FleetInformationDto = {
ids: ["1", "2"],
dateFrom: '2021-01-01',
dateTo: '2021-02-01'
}
const queueData: ReportQueue = {
action: ReportQueueAction.GENERATE,
type: ReportQueueType.FLEET_INFORMATION,
dto: dto
}
// ^ works as expected
但是如果我们将“VehicleInformationDto”添加到类型 FLEET_INFORMATION 它不会抛出错误
const dto2: VehicleInformationDto = {
ids: ["1", "2"],
dateFrom: '2021-01-01',
dateTo: '2021-02-01'
}
const queueData2: ReportQueue = {
action: ReportQueueAction.GENERATE,
type: ReportQueueType.FLEET_INFORMATION,
dto: dto2 // <-- no error thrown here
}
嗯,这里有什么问题?我错过了什么吗?
问题:为什么我能够将 VehicleInformationDto
分配给 queueData2
中的 dto
而 typescript 期望它是 FleetInformationDto
?
编辑:好的,是的,这是因为它们共享相同的属性,那么,我该如何添加检查呢?
最佳答案
typescript 是 structurally typed, not nominally typed .这意味着就 Typescript 而言,这些是相同类型:
class FleetInformationDto extends GenericDto { }
class VehicleInformationDto extends GenericDto { }
虽然我认为这绝对是向 Javascript 这样的语言添加静态类型的正确选择,其中对象是属性的集合,但它可能会导致一些微妙的问题:
interface Vec2 {
x: number
y: number
}
interface Vec3 {
x: number
y: number
z: number
}
const m = { x: 0, y: 0, z: "hello world" };
const n: Vec2 = m; // N.B. structurally m qualifies as Vec2!
function f(x: Vec2 | Vec3) {
if (x.z) return x.z.toFixed(2); // This fails if z is not a number!
}
f(n); // compiler must allow this call
这里我们正在做一些图形编程,并且有 2D 和 3D 向量,但是我们有一个问题:对象可以有额外的属性并且仍然在结构上限定,这导致了这个联合类型的问题(听起来很熟悉?)。
在您的特定情况下,答案是使用 discriminant轻松区分联合中的相似类型:
interface FleetInformationDto extends GenericDto {
// N.B., fleet is a literal *type*, not a string literal
// *value*.
kind: 'fleet'
}
interface VehicleInformationDto extends GenericDto {
kind: 'vehicle'
}
这里我使用了字符串,但任何唯一的编译时常量(任何原始值或 enum
的成员)都可以。此外,由于您没有实例化您的类并纯粹将它们用作类型,我已将它们设为接口(interface),但适用相同的原则。
现在您可以清楚地看到“车队”类型无法分配给“车辆”类型的错误。
关于 typescript 联合类型不会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70519214/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!