(O: object, hint: T)
| 13 | } |
| 14 | |
| 15 | function OrdinaryToPrimitive< |
| 16 | T extends 'string' | 'number' = 'string' | 'number', |
| 17 | >(O: object, hint: T): string | number | boolean | undefined | null { |
| 18 | let methodNames: Array<'toString' | 'valueOf'> |
| 19 | if (hint === 'string') { |
| 20 | methodNames = ['toString', 'valueOf'] |
| 21 | } else { |
| 22 | methodNames = ['valueOf', 'toString'] |
| 23 | } |
| 24 | for (const name of methodNames) { |
| 25 | const method = O[name] |
| 26 | if (IsCallable(method)) { |
| 27 | let result = method.call(O) |
| 28 | if (typeof result !== 'object') { |
| 29 | return result |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | throw new TypeError('Cannot convert object to primitive value') |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * https://tc39.es/ecma262/#sec-toprimitive |
no test coverage detected