This is a collection of answers to some common questions that have arisen when using TypeScript in our projects, especially from developers who are new to the language.
See the TypeScript Handbook.
For code that is authored in JS and annotated using JSDoc comments, see the JSDoc page and checking JavaScript files page.
Put @ts-ignore
in a comment to ignore the next line.
Props are described using TypeScript and documented using JSDoc, like so:
export type MyButtonProps = {
/** Text label for the button. */
label: string;
};
export default function MyButton({ label }: MyButtonProps) {
// Content here
}
For small helper components which are not exported from a module, you can, optionally, use a more succinct "inline" syntax:
function MyButton({ label }: { label: string }) {
}
Given code like this:
someElement.setAttribute('attr', 42)
TS will complain that attribute values must be strings, although the code would actually work as the value would be coerced at runtime. To keep TS happy, convert the value explicitly:
someElement.setAttribute('attr', myNumber.toString())
Sometimes will warn that a value might be null or undefined in a certain context and that this case is not handled.
First, have a think about whether TS is right. If so, add a check for this.
Sometimes you may know that a value cannot be null for reasons that may not be obvious from a glance at the code. In this case you can use the !
operator
or a value as T
type cast.
For example, supposing you have an HTMLElement and you try to access its parentElement
. In general, that property can be null if the element has no parent. You might know that this cannot be the case in a certain situation though:
const parentEl = someElement.parentElement!;