let name: string = 'John';
let age: number = 30;
let isStudent: boolean = true;
interface Person {
name: string;
age: number;
}
type Student = {
name: string;
grade: string;
};
type ID = string | number;
type Coordinates = [number, number];
interface Animal {
name: string;
}
interface Dog extends Animal {
breed: string;
}
enum Color {
Red,
Green,
Blue,
}
let myColor: Color = Color.Green;
function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>('myString');
type Union = string | number;
type Intersection = { id: number } & { name: string };
interface User {
id: number;
name: string;
age: number;
}
type PartialUser = Partial<User>;
// PartialUser is { id?: number; name?: string; age?: number; }
type UserWithoutAge = Omit<User, 'age'>;
// UserWithoutAge is { id: number; name: string; }
type UserName = Pick<User, 'name'>;
// UserName is { name: string; }
tsconfig.json is a configuration file for TypeScript projects.
It specifies the root files and the compiler options required to compile the project.
tsconfig.json'
{
"compilerOptions": {
// Compiler options go here
},
"include": [
// List of files to include
],
"exclude": [
// List of files to exclude
]
}
"module": "commonjs"
"strict": true
"jsx": "react"
"target": "es5"
"moduleResolution": "node"
"esModuleInterop": true
"skipLibCheck": true
"forceConsistentCasingInFileNames": true
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"jsx": "react",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
Props
interface Props {
title: string;
}
const Header: React.FC<Props> = ({ title }) => {
return <h1>{title}</h1>;
};
State
const [count,setCount]= useState<number>(2)