Hi Guys Good Day!
The | operator tells that we can have either one of the types or values on the right-hand expression.
In this example the LogLevel type means that we can have a value of "error","fatal","info" or "debug".
In the above example, we made a function called myLogger that has two parameters level which has a type of LogLevel and message that can have a value of either a string or an object.
We invoke the myLogger function with values "err" and "Wtf some error occurred" for level and message parameters respectively. If you're using VS Code as your editor a compile-time error will be visible with a message of "Argument of type '"err"' is not assignable to parameter of type 'LogLevel'.". Obviously what this means is that "err" value is not compatible with our type LogLevel.
But if we change "err" to "error" the compile-time error will be gone.
The & operator tells that we can combine the values or types on the right-hand expression.
In the above example, we have two interfaces APIResponse and APIData.
and a function called reply. The resp parameter has the combined type of APIResponse and APIData which means that we can have all the properties or members of both interfaces in our resp parameter.
In the above example, we made a variable resp that has a combined type of APIResponse and APIData. Then we specify a member wahhhhh_ in __resp object that does not exist in either in both interfaces. A compile-error will be visible with a message of Type '{ ResponseCode: number; ResponseMessage: string; orders: undefined[]; wahhhhh: undefined[]; }' is not assignable to type 'APIResponse & APIData'.
Object literal may only specify known properties, and 'wahhhhh' does not exist in type 'APIResponse & APIData'.. This indicates that the 'wahhhh' property does not exist in both interfaces obviously.
But if we change "wahhhhh" to "products" the compile-time error will be gone.
Thanks guys for reading this post.
Top comments (0)