index.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { ITypeSuite, TType } from "./types";
  2. import { IErrorDetail } from "./util";
  3. /**
  4. * Export functions used to define interfaces.
  5. */
  6. export { TArray, TEnumType, TEnumLiteral, TFunc, TIface, TLiteral, TName, TOptional, TParam, TParamList, TProp, TTuple, TType, TUnion, TIntersection, array, enumlit, enumtype, func, iface, lit, name, opt, param, tuple, union, intersection, BasicType, ITypeSuite, } from "./types";
  7. export { VError, IErrorDetail } from './util';
  8. export interface ICheckerSuite {
  9. [name: string]: Checker;
  10. }
  11. /**
  12. * Takes one of more type suites (e.g. a module generated by `ts-interface-builder`), and combines
  13. * them into a suite of interface checkers. If a type is used by name, that name should be present
  14. * among the passed-in type suites.
  15. *
  16. * The returned object maps type names to Checker objects.
  17. */
  18. export declare function createCheckers(...typeSuite: ITypeSuite[]): ICheckerSuite;
  19. /**
  20. * Checker implements validation of objects, and also includes accessors to validate method calls.
  21. * Checkers should be created using `createCheckers()`.
  22. */
  23. export declare class Checker {
  24. private suite;
  25. private ttype;
  26. private _path;
  27. private props;
  28. private checkerPlain;
  29. private checkerStrict;
  30. constructor(suite: ITypeSuite, ttype: TType, _path?: string);
  31. /**
  32. * Set the path to report in errors, instead of the default "value". (E.g. if the Checker is for
  33. * a "person" interface, set path to "person" to report e.g. "person.name is not a string".)
  34. */
  35. setReportedPath(path: string): void;
  36. /**
  37. * Check that the given value satisfies this checker's type, or throw Error.
  38. */
  39. check(value: any): void;
  40. /**
  41. * A fast check for whether or not the given value satisfies this Checker's type. This returns
  42. * true or false, does not produce an error message, and is fast both on success and on failure.
  43. */
  44. test(value: any): boolean;
  45. /**
  46. * Returns an error object describing the errors if the given value does not satisfy this
  47. * Checker's type, or null if it does.
  48. */
  49. validate(value: any): IErrorDetail | null;
  50. /**
  51. * Check that the given value satisfies this checker's type strictly. This checks that objects
  52. * and tuples have no extra members. Note that this prevents backward compatibility, so usually
  53. * a plain check() is more appropriate.
  54. */
  55. strictCheck(value: any): void;
  56. /**
  57. * A fast strict check for whether or not the given value satisfies this Checker's type. Returns
  58. * true or false, does not produce an error message, and is fast both on success and on failure.
  59. */
  60. strictTest(value: any): boolean;
  61. /**
  62. * Returns an error object describing the errors if the given value does not satisfy this
  63. * Checker's type strictly, or null if it does.
  64. */
  65. strictValidate(value: any): IErrorDetail | null;
  66. /**
  67. * If this checker is for an interface, returns a Checker for the type required for the given
  68. * property of this interface.
  69. */
  70. getProp(prop: string): Checker;
  71. /**
  72. * If this checker is for an interface, returns a Checker for the argument-list required to call
  73. * the given method of this interface. E.g. if this Checker is for the interface:
  74. * interface Foo {
  75. * find(s: string, pos?: number): number;
  76. * }
  77. * Then methodArgs("find").check(...) will succeed for ["foo"] and ["foo", 3], but not for [17].
  78. */
  79. methodArgs(methodName: string): Checker;
  80. /**
  81. * If this checker is for an interface, returns a Checker for the return value of the given
  82. * method of this interface.
  83. */
  84. methodResult(methodName: string): Checker;
  85. /**
  86. * If this checker is for a function, returns a Checker for its argument-list.
  87. */
  88. getArgs(): Checker;
  89. /**
  90. * If this checker is for a function, returns a Checker for its result.
  91. */
  92. getResult(): Checker;
  93. /**
  94. * Return the type for which this is a checker.
  95. */
  96. getType(): TType;
  97. /**
  98. * Actual implementation of check() and strictCheck().
  99. */
  100. private _doCheck;
  101. private _doValidate;
  102. private _getMethod;
  103. }
  104. /**
  105. * Typed checker interface. Adds type guard functionality to a normal `Checker`.
  106. *
  107. * To use, cast a `Checker` to a `CheckerT<>` using the appropriate type.
  108. *
  109. * eg.
  110. * import { MyInterface } from './my-interface';
  111. * import MyInterfaceTi from './my-interface-ti';
  112. *
  113. * const checkers = createCheckers(MyInterfaceTi) as {
  114. * MyInterface: CheckerT<MyInterface>
  115. * };
  116. *
  117. * TODO:
  118. * - Enable `check()` and `strictCheck()` type assertion definitions once the functionality
  119. * is correctly working in TypeScript. (https://github.com/microsoft/TypeScript/issues/36931)
  120. */
  121. export interface CheckerT<T> extends Checker {
  122. test(value: any): value is T;
  123. strictTest(value: any): value is T;
  124. }