webidl.d.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // These types are not exported, and are only used internally
  2. /**
  3. * Take in an unknown value and return one that is of type T
  4. */
  5. type Converter<T> = (object: unknown) => T
  6. type SequenceConverter<T> = (object: unknown, iterable?: IterableIterator<T>) => T[]
  7. type RecordConverter<K extends string, V> = (object: unknown) => Record<K, V>
  8. interface ConvertToIntOpts {
  9. clamp?: boolean
  10. enforceRange?: boolean
  11. }
  12. interface WebidlErrors {
  13. exception (opts: { header: string, message: string }): TypeError
  14. /**
  15. * @description Throw an error when conversion from one type to another has failed
  16. */
  17. conversionFailed (opts: {
  18. prefix: string
  19. argument: string
  20. types: string[]
  21. }): TypeError
  22. /**
  23. * @description Throw an error when an invalid argument is provided
  24. */
  25. invalidArgument (opts: {
  26. prefix: string
  27. value: string
  28. type: string
  29. }): TypeError
  30. }
  31. interface WebidlUtil {
  32. /**
  33. * @see https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
  34. */
  35. Type (object: unknown):
  36. | 'Undefined'
  37. | 'Boolean'
  38. | 'String'
  39. | 'Symbol'
  40. | 'Number'
  41. | 'BigInt'
  42. | 'Null'
  43. | 'Object'
  44. /**
  45. * @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  46. */
  47. ConvertToInt (
  48. V: unknown,
  49. bitLength: number,
  50. signedness: 'signed' | 'unsigned',
  51. opts?: ConvertToIntOpts
  52. ): number
  53. /**
  54. * @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  55. */
  56. IntegerPart (N: number): number
  57. /**
  58. * Stringifies {@param V}
  59. */
  60. Stringify (V: any): string
  61. }
  62. interface WebidlConverters {
  63. /**
  64. * @see https://webidl.spec.whatwg.org/#es-DOMString
  65. */
  66. DOMString (V: unknown, prefix: string, argument: string, opts?: {
  67. legacyNullToEmptyString: boolean
  68. }): string
  69. /**
  70. * @see https://webidl.spec.whatwg.org/#es-ByteString
  71. */
  72. ByteString (V: unknown, prefix: string, argument: string): string
  73. /**
  74. * @see https://webidl.spec.whatwg.org/#es-USVString
  75. */
  76. USVString (V: unknown): string
  77. /**
  78. * @see https://webidl.spec.whatwg.org/#es-boolean
  79. */
  80. boolean (V: unknown): boolean
  81. /**
  82. * @see https://webidl.spec.whatwg.org/#es-any
  83. */
  84. any <Value>(V: Value): Value
  85. /**
  86. * @see https://webidl.spec.whatwg.org/#es-long-long
  87. */
  88. ['long long'] (V: unknown): number
  89. /**
  90. * @see https://webidl.spec.whatwg.org/#es-unsigned-long-long
  91. */
  92. ['unsigned long long'] (V: unknown): number
  93. /**
  94. * @see https://webidl.spec.whatwg.org/#es-unsigned-long
  95. */
  96. ['unsigned long'] (V: unknown): number
  97. /**
  98. * @see https://webidl.spec.whatwg.org/#es-unsigned-short
  99. */
  100. ['unsigned short'] (V: unknown, opts?: ConvertToIntOpts): number
  101. /**
  102. * @see https://webidl.spec.whatwg.org/#idl-ArrayBuffer
  103. */
  104. ArrayBuffer (V: unknown): ArrayBufferLike
  105. ArrayBuffer (V: unknown, opts: { allowShared: false }): ArrayBuffer
  106. /**
  107. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  108. */
  109. TypedArray (
  110. V: unknown,
  111. TypedArray: NodeJS.TypedArray | ArrayBufferLike
  112. ): NodeJS.TypedArray | ArrayBufferLike
  113. TypedArray (
  114. V: unknown,
  115. TypedArray: NodeJS.TypedArray | ArrayBufferLike,
  116. opts?: { allowShared: false }
  117. ): NodeJS.TypedArray | ArrayBuffer
  118. /**
  119. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  120. */
  121. DataView (V: unknown, opts?: { allowShared: boolean }): DataView
  122. /**
  123. * @see https://webidl.spec.whatwg.org/#BufferSource
  124. */
  125. BufferSource (
  126. V: unknown,
  127. opts?: { allowShared: boolean }
  128. ): NodeJS.TypedArray | ArrayBufferLike | DataView
  129. ['sequence<ByteString>']: SequenceConverter<string>
  130. ['sequence<sequence<ByteString>>']: SequenceConverter<string[]>
  131. ['record<ByteString, ByteString>']: RecordConverter<string, string>
  132. [Key: string]: (...args: any[]) => unknown
  133. }
  134. export interface Webidl {
  135. errors: WebidlErrors
  136. util: WebidlUtil
  137. converters: WebidlConverters
  138. /**
  139. * @description Performs a brand-check on {@param V} to ensure it is a
  140. * {@param cls} object.
  141. */
  142. brandCheck <Interface>(V: unknown, cls: Interface, opts?: { strict?: boolean }): asserts V is Interface
  143. /**
  144. * @see https://webidl.spec.whatwg.org/#es-sequence
  145. * @description Convert a value, V, to a WebIDL sequence type.
  146. */
  147. sequenceConverter <Type>(C: Converter<Type>): SequenceConverter<Type>
  148. illegalConstructor (): never
  149. /**
  150. * @see https://webidl.spec.whatwg.org/#es-to-record
  151. * @description Convert a value, V, to a WebIDL record type.
  152. */
  153. recordConverter <K extends string, V>(
  154. keyConverter: Converter<K>,
  155. valueConverter: Converter<V>
  156. ): RecordConverter<K, V>
  157. /**
  158. * Similar to {@link Webidl.brandCheck} but allows skipping the check if third party
  159. * interfaces are allowed.
  160. */
  161. interfaceConverter <Interface>(cls: Interface): (
  162. V: unknown,
  163. opts?: { strict: boolean }
  164. ) => asserts V is typeof cls
  165. // TODO(@KhafraDev): a type could likely be implemented that can infer the return type
  166. // from the converters given?
  167. /**
  168. * Converts a value, V, to a WebIDL dictionary types. Allows limiting which keys are
  169. * allowed, values allowed, optional and required keys. Auto converts the value to
  170. * a type given a converter.
  171. */
  172. dictionaryConverter (converters: {
  173. key: string,
  174. defaultValue?: () => unknown,
  175. required?: boolean,
  176. converter: (...args: unknown[]) => unknown,
  177. allowedValues?: unknown[]
  178. }[]): (V: unknown) => Record<string, unknown>
  179. /**
  180. * @see https://webidl.spec.whatwg.org/#idl-nullable-type
  181. * @description allows a type, V, to be null
  182. */
  183. nullableConverter <T>(
  184. converter: Converter<T>
  185. ): (V: unknown) => ReturnType<typeof converter> | null
  186. argumentLengthCheck (args: { length: number }, min: number, context: string): void
  187. }