首页 > 解决方案 > 打字稿:多个可能的接口作为一个接口

问题描述

如果我有这样的变量:

let x: number | string | undefined;

是否可以创建一个支持所有这些类型的接口?就像是:

let x: MyCustomInterface;

谢谢!

标签: typescriptinterface

解决方案


不,接口本身不支持联合。您可以创建一个类型别名:

type MyCustomType = number | string | undefined;
let x: MyCustomType;

推荐阅读