Casing π
Weβve have been following some variant of casing in our javascript code since forever, like camelCase for variable names, etc. a similar convention has to be followed in typescript type naming so as to be able to differentiate between a javascript names (run time) and types (compile time) names as well as satisfy the code editor highlighting. It is generally recommended and followed across the industry to follow PascalCase naming for types and interfaces in typescript. Letβs understand with an example. In the example code below you can see thereβs a slight conflict in the names for the type fruit vs the variable name.
Descriptive ποΈ
Type names should accurately describe the purpose and content of the type. For instance, in this example the type should clearly mention it refers to the react component types, and at the same time also define for which react component the type is for.
Avoid I and T π π»ββοΈ
Avoid prefixes like I and T you might have seen some codebase writing their type names with I or T prefix, denoting that they are interfaces or types respectively, but actually if you think more deeply they donβt add any values at all, moreover it looks ugly to me personally. Also, think of a case when you change it from a type to an interface, would you be going to each instances and renaming it. I would suggest just avoid these prefixes.
Generic Type Params π₯
Just naming your generic type params as T
or U
can be very confusing, you might have seen people following conventions for naming their generic type params like T, U, V etc. it is as similar to just naming your function arguments as arg1
, arg2
, arg3
- you canβt take out enough values out of it.
Donβt Pluralize πΎ
Many times, we declare types referring to a bunch of values, like fruits or something, but actually the type is not resembling multiple values, but a value out of them, letβs see from an example -
Until next time ππ½