typescript - 有没有办法创建一个项目的类型取决于第二个的界面?
问题描述
例如,如果我有这个对象:
{a, b}
我想确保如果a
是'a',b
应该是一个数字,如果a
是'b',b应该是一个字符串,如果a
是'c',b应该是一个日期。
如果我将界面拆分为三个选项:
interface {
a: 'a'
b: number
}
interface {
a: 'b'
b: string
}
interface {
a: 'c'
b: Date
}
解决方案
你很接近:
interface A {
a: 'a'
b: number
}
interface B {
a: 'b'
b: string
}
interface C {
a: 'c'
b: Date
}
type Union = A | B | C
推荐阅读
- clojurescript - Can i use (if) as a form-2 reagent render-function
- apache - How to replace a standard httpd package with httpd24-httpd from Software Collections?
- php - 如何将关联的php数组json_encode为[{x:key1,y:value1},{x:key2,y:value2},...]?
- kotlin - 如何使用带有命名参数和默认值的自定义设置器
- asp.net-core-mvc - 列名无效 Departments.DepartmentID
- python - Is there a way to export a series of scenes rendered in Panda3D as individual image files?
- asp.net-mvc - Adding Viewbag in Current view
- apache-poi - Apache POI Date Parsing One Second Off
- python - How to read csv file pieces and put into single pandas dataframe?
- python-3.x - SELECT statement returning the column name instead of the VALUE (for that said column)