如何指定一个必须具有特定类型的值而不为其创建变量?

时间:2018-06-21 14:06:50

标签: typescript styled-components emotion

说我有

type TypeMsAnimationDuration =
  | 75
  | 100
  | 150
  | 200
  | 250
  | 300
  | 350
  | 500;

const cssInJs = css`transition: all ${999}ms;`

如何确保值“ 999”是TypeMsAnimationDuration的值?

1 个答案:

答案 0 :(得分:0)

如果不将其放在变量中或创建需要调用以验证值的辅助函数,则无法执行此操作:

type TypeMsAnimationDuration =
| 75
| 100
| 150
| 200
| 250
| 300
| 350
| 500;

function animDuration(ms: TypeMsAnimationDuration) {
    return ms;
}
const cssInJs = css`transition: all ${animDuration(999)}ms;` // Error
const cssInJs = css`transition: all ${animDuration(250)}ms;` // Ok