[Typescript] Use never for readable string

const demoFunc = <TObj extends {}>(obj: TObj, key: ErrorIfNever<keyof TObj, `You much pass at least one key`>) => {
  return obj[key as keyof TObj]
}

type ErrorIfNever<T, TError> = [T] extends [never] ? TError: T;

demoFunc({}, 'example')

 

posted @ 2023-03-30 00:29  Zhentiw  阅读(14)  评论(0编辑  收藏  举报