[TypeScript] Ensure params not accept certain type

Let's say we have a function accept any type but array.

function anyTypesButNotArray<T>(
  val: T extends any[] ? "you should not pass in array" : T
) {
  return val;
}

anyTypesButNotArray(["sf", "sdf"]);

 

 

posted @ 2022-04-22 16:20  Zhentiw  阅读(25)  评论(0编辑  收藏  举报