[Typescript] Use never for readable string
typescript string for use
2023-09-14 08:59:11 时间
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')
相关文章
- [Typescript] 117. Hard - ClassPublicKeys
- [Typescript] 115. Hard - Drop String
- [Typescript] 103. Hard - Tuple Filter
- [Typescript] 76. Easy - Parameters
- [Typescript] TypeScript module Augmentation
- [Typescript] Tips: Create your own 'objectKeys' function using generics and the 'keyof' operator
- [Typescript] 31. Medium - Length of String
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [TypeScript] String Enums and Inlining Members
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
- [Typescript] 135. Easy - Maybe helper
- [Typescript] 112. Hard - DeepPick
- [Typescript] Get a TypeScript package ready for release to NPM
- [Typescript] 31. Medium - Length of String
- [Typescript Challenges] 6 Easy - Exclude
- [Typescript] Making TypeScript Stick - 3
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [Typescript] Statically Type Unknown Values with TypeScript's unknown Type
- [Typescript] Create Type From Any Given Data in TypeScript
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
- TypeScript里string和String,真不是仅仅是大小写的区别