New unknown top type

TypeScript 3.0 introduces a new top type unknown.unknown is the type-safe counterpart of any.Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing.Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

Example
  1. // In an intersection everything absorbs unknown
  2. type T00 = unknown & null; // null
  3. type T01 = unknown & undefined; // undefined
  4. type T02 = unknown & null & undefined; // null & undefined (which becomes never)
  5. type T03 = unknown & string; // string
  6. type T04 = unknown & string[]; // string[]
  7. type T05 = unknown & unknown; // unknown
  8. type T06 = unknown & any; // any
  9. // In a union an unknown absorbs everything
  10. type T10 = unknown | null; // unknown
  11. type T11 = unknown | undefined; // unknown
  12. type T12 = unknown | null | undefined; // unknown
  13. type T13 = unknown | string; // unknown
  14. type T14 = unknown | string[]; // unknown
  15. type T15 = unknown | unknown; // unknown
  16. type T16 = unknown | any; // any
  17. // Type variable and unknown in union and intersection
  18. type T20<T> = T & {}; // T & {}
  19. type T21<T> = T | {}; // T | {}
  20. type T22<T> = T & unknown; // T
  21. type T23<T> = T | unknown; // unknown
  22. // unknown in conditional types
  23. type T30<T> = unknown extends T ? true : false; // Deferred
  24. type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
  25. type T32<T> = never extends T ? true : false; // true
  26. type T33<T> = T extends never ? true : false; // Deferred
  27. // keyof unknown
  28. type T40 = keyof any; // string | number | symbol
  29. type T41 = keyof unknown; // never
  30. // Only equality operators are allowed with unknown
  31. function f10(x: unknown) {
  32. x == 5;
  33. x !== 10;
  34. x >= 0; // Error
  35. x + 1; // Error
  36. x * 2; // Error
  37. -x; // Error
  38. +x; // Error
  39. }
  40. // No property accesses, element accesses, or function calls
  41. function f11(x: unknown) {
  42. x.foo; // Error
  43. x[5]; // Error
  44. x(); // Error
  45. new x(); // Error
  46. }
  47. // typeof, instanceof, and user defined type predicates
  48. declare function isFunction(x: unknown): x is Function;
  49. function f20(x: unknown) {
  50. if (typeof x === "string" || typeof x === "number") {
  51. x; // string | number
  52. }
  53. if (x instanceof Error) {
  54. x; // Error
  55. }
  56. if (isFunction(x)) {
  57. x; // Function
  58. }
  59. }
  60. // Homomorphic mapped type over unknown
  61. type T50<T> = { [P in keyof T]: number };
  62. type T51 = T50<any>; // { [x: string]: number }
  63. type T52 = T50<unknown>; // {}
  64. // Anything is assignable to unknown
  65. function f21<T>(pAny: any, pNever: never, pT: T) {
  66. let x: unknown;
  67. x = 123;
  68. x = "hello";
  69. x = [1, 2, 3];
  70. x = new Error();
  71. x = x;
  72. x = pAny;
  73. x = pNever;
  74. x = pT;
  75. }
  76. // unknown assignable only to itself and any
  77. function f22(x: unknown) {
  78. let v1: any = x;
  79. let v2: unknown = x;
  80. let v3: object = x; // Error
  81. let v4: string = x; // Error
  82. let v5: string[] = x; // Error
  83. let v6: {} = x; // Error
  84. let v7: {} | null | undefined = x; // Error
  85. }
  86. // Type parameter 'T extends unknown' not related to object
  87. function f23<T extends unknown>(x: T) {
  88. let y: object = x; // Error
  89. }
  90. // Anything but primitive assignable to { [x: string]: unknown }
  91. function f24(x: { [x: string]: unknown }) {
  92. x = {};
  93. x = { a: 5 };
  94. x = [1, 2, 3];
  95. x = 123; // Error
  96. }
  97. // Locals of type unknown always considered initialized
  98. function f25() {
  99. let x: unknown;
  100. let y = x;
  101. }
  102. // Spread of unknown causes result to be unknown
  103. function f26(x: {}, y: unknown, z: any) {
  104. let o1 = { a: 42, ...x }; // { a: number }
  105. let o2 = { a: 42, ...x, ...y }; // unknown
  106. let o3 = { a: 42, ...x, ...y, ...z }; // any
  107. }
  108. // Functions with unknown return type don't need return expressions
  109. function f27(): unknown {
  110. }
  111. // Rest type cannot be created from unknown
  112. function f28(x: unknown) {
  113. let { ...a } = x; // Error
  114. }
  115. // Class properties of type unknown don't need definite assignment
  116. class C1 {
  117. a: string; // Error
  118. b: unknown;
  119. c: any;
  120. }