/** * The identity function. * * Help to build an object of type T. * Better than using 'as T' as there is no type safety loss. * * - Used as continence for enabling type inference. * Example: * * type Circle = { * type: "CIRCLE"; * radius: number; * }; * * type Square = { * type: "SQUARE"; * side: number; * }; * type Shape= Circle | Square; * * declare function f(shape: Shape): void; * * f(id({ "type": "CIRCLE", "radius": 33 }); <== We have auto completion to instantiate circle. * * - Used to loosen the type restriction without saying "trust me" to the compiler. * declare const x: Set; * declare function f(s: Set): void; * f(id>(x)); * * Example: * declare const x: Set; * declare f(x: Set): void; * id(x as Set<["FOO"]>); <== trust me it's readonly! * f(id>(x)); <== we acknowledge that we are out of the safe zone. */ export const id = (x: T) => x;