允许其他属性的 TypeScript 接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33836671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:49:31  来源:igfitidea点击:

TypeScript interface that allows other properties

typescript

提问by Aaron Beall

In summary, is it possible to have an interface that declares some base properties, but does not restrict additional properties? This is my current situation:

总之,是否有可能有一个接口声明一些基本属性,但不限制其他属性?这是我目前的情况:

I'm using the Flux pattern, which defines a generic dispatcher:

我正在使用Flux 模式,它定义了一个通用调度程序:

class Dispatcher<TPayload> {
    dispatch(arg:TPayload):void { }
}

I then create a dispatcher with my own payload type, like this:

然后我使用我自己的有效负载类型创建一个调度程序,如下所示:

interface ActionPayload {
    actionType: string
}

const dispatcher = new Dispatcher<ActionPayload>();

Now I have some action code that should dispatch a payload with some additional data, but the ActionPayloadinterface only allows for actionType. In other words, this code:

现在我有一些动作代码应该发送带有一些附加数据的有效负载,但ActionPayload接口只允许actionType. 换句话说,这段代码:

interface SomePayload extends ActionPayload {
    someOtherData: any
}

class SomeActions {
    doSomething():void {
        dispatcher.dispatch({
            actionType: "hello",
            someOtherData: {}
        })
    }
}

Gives a compile-error because someOtherDatadoes not match the ActionPayloadinterface. The issue is that many different "action" classes will re-use the same dispatcher, so while it's someOtherDatahere it might be anotherKindOfDataover there, and so on. At the moment, all I can do to accomodate this is use new Dispatcher<any>()because different actions will be dispatched. All actions share a base ActionPayload, though, so I was hoping to be able to constrain the type like new Dispatcher<extends ActionPayload>()or something. Is something like that possible?

给出编译错误,因为someOtherDataActionPayload接口不匹配。问题是许多不同的“动作”类将重用同一个调度程序,所以虽然它在someOtherData这里,但它可能anotherKindOfData在那里,等等。目前,我所能做的就是适应这一点,new Dispatcher<any>()因为将分派不同的动作。ActionPayload不过,所有动作都共享一个 base ,所以我希望能够限制像之类的类型new Dispatcher<extends ActionPayload>()。这样的事情可能吗?

回答by Sebastien

If you want ActionPayload to accept any other property you can add an indexer:

如果您希望 ActionPayload 接受任何其他属性,您可以添加索引器:

interface ActionPayload {
    actionType: string,
    [x: string]: any 
}

See https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#strict-object-literal-assignment-checking

请参阅https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#strict-object-literal-assignment-checking

回答by Aaron Beall

I think I found what I was looking for. I can cast the dispatched object to SomePayload, and TSC validates that its compatible with both the cast interface and the TPayloadof the dispatcher:

我想我找到了我要找的东西。我可以将调度的对象SomePayload强制转换为,并且 TSC 验证它与强制转换接口和TPayload调度程序的兼容:

    dispatcher.dispatch(<SomePayload>{
        actionType: "hello",
        someOtherData: {}
    })

Example online.

在线示例。