1. What is React.FC ?
2. Function Arguments
Like we do with normal functions in Typescript
We can, pretty much do the same thing with functional components
3. unknown vs any
this will complain, because we are returning any and it will not cause error (which it should, because someArgs could be a string or a bool here).
To fix this, we will use unknown instead of any.
4. Generics
Generics allow you to create reusable components and functions that work with different types.
Here, T is a generic type parameter. The test function will accept an argument of type T and return a value of the same type.
Type Inference: TypeScript can infer the type of T based on the argument passed to the function.
In this case, TypeScript infers that T is a number, so result will be of type number.
Explicit Type Specification: You can also explicitly specify the type when calling the function.
This works because we are passing a string to the function that expects a string.
Example of Type Error If you specify a type that does not match the argument type, TypeScript will throw an error.
This will throw an error because the test function is explicitly told to expect a string, but a number is being passed as an argument.
Example with Operation Let's consider an example where generics make more sense with a specific operation:
Using the identity function with inferred types:
Using the identity function with explicit types:
Incorrect type specification: