This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Babel for Fast Refresh
- @vitejs/plugin-react-swc uses SWC for Fast Refresh
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
- Configure the top-level
parserOptions
property like this:
export default {
// other rules...
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: __dirname,
},
}
- Replace
plugin:@typescript-eslint/recommended
toplugin:@typescript-eslint/recommended-type-checked
orplugin:@typescript-eslint/strict-type-checked
- Optionally add
plugin:@typescript-eslint/stylistic-type-checked
- Install eslint-plugin-react and add
plugin:react/recommended
&plugin:react/jsx-runtime
to theextends
list
In TypeScript, number
is used to define variables that hold numeric values.
let age: number = 25;
The string type is used to define variables that hold textual data.
let name: string = "Hardik";
The boolean type is used to define variables that hold true or false values.
let isDeveloper: boolean = true;
An array in TypeScript can be defined to hold elements of a specific type.
An array in TypeScript can be defined to hold elements of a specific type.
Tuples allow you to express an array with a fixed number of elements whose types are known.
let user: [string, number] = ["Hardik", 25];
Enums allow a developer to define a set of named constants. TypeScript provides both numeric and string-based enums.
enum Color {
Red,
Green,
Blue
}
let c: Color = Color.Green;
The any type allows a variable to hold values of any type, essentially opting out of type checking.
let something: any = "Hello";
something = 42;
void
is used to indicate that a function does not return any value.
function logMessage(message: string): void { console.log(message); }
null and undefined are subtypes of all other types and can be assigned to any type.
let u: undefined = undefined;
let n: null = null;
The never type represents the type of values that never occur. For example, a function that always throws an exception or one that never returns.
function error(message: string): never {
throw new Error(message);
}
The object type represents non-primitive types.
let person: { name: string; age: number } = {
name: "Hardik",
age: 25,
};
Type aliases create a new name for a type. They are useful for simplifying complex type definitions.
type Point = {
x: number;
y: number;
};
let point: Point = { x: 10, y: 20 };
Interfaces define the structure of an object and can be implemented by classes or used to type-check an object.
interface Person {
name: string;
age: number;
}
let person: Person = {
name: "Hardik",
age: 25,
};
Union types allow a variable to hold values of multiple types.
let id: number | string;
id = 10;
id = "10";
Intersection types combine multiple types into one. This is useful when you want to merge properties from different types into a single type.
type Drivable = {
drive(): void;
};
type Flyable = {
fly(): void;
};
type Vehicle = Drivable & Flyable;
let carPlane: Vehicle = {
drive: () => console.log("Driving"),
fly: () => console.log("Flying")
};
Interfaces can be extended to create new interfaces with additional properties. This is useful for creating hierarchies and reusing common properties.
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
let myDog: Dog = {
name: "Buddy",
breed: "Golden Retriever"
};
Generics provide a way to create reusable components that can work with a variety of types.
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString");
The typeof operator returns a string indicating the type of the unevaluated operand.
let age: number = 25;
console.log(typeof age); // "number"
let name: string = "Hardik";
console.log(typeof name); // "string"
The keyof operator takes an object type and produces a string or numeric literal union of its keys.
interface Person {
name: string;
age: number;
}
type PersonKeys = keyof Person; // "name" | "age"
The typeof type query operator is used to obtain the type of a variable or object.
let person = {
name: "Hardik",
age: 25,
};
type PersonType = typeof person;
// PersonType is { name: string; age: number; }
The instanceof operator checks whether an object is an instance of a specific class.
class Car {
model: string;
constructor(model: string) {
this.model = model;
}
}
let myCar = new Car("Toyota");
console.log(myCar instanceof Car); // true