let varUnknown: unknown; let varAny: any;
let varString: string = "Hello World"; let varBoolean: boolean = true; let varNumber: number = 10; let varArray: string[] = ['one', 'two', 'three'];
const object: {[key: string]: any} = {} object.key = "value";
let varObject: { one: string; two?: boolean; // optional callback(): void; } = { one: 'one', callback: () => { } };
interface IVarObject { one: string; two?: boolean; // optional } const varObject: IVarObject = { one: 'one' };
function consoleLogMessage(message: any): void { console.log(message); } consoleLogMessage('Message'); // Message
function add(x: number, y: number): number { return x + y; } add(1, 2); // 3
function addFromObject({ x, y }: { x: number; y: number }): number { return x + y; } addFromObject({ x: 1, y: 2 }); // 3
const varNull: null = null; // useless const varUndefined: undefined; // useless
const varArray: unknown[] = ['one', true, 3]; const varArray: any[] = ['one', true, 3];
const varFunction: {} = () => {};
const varFunctionArray = function (varArray: string[]) { return varArray; } varFunctionArray(['one', 'two', 'three']);
const varFunctionObject = function (param1: string, param2: {}) { } varFunctionObject('one', { one: 'one', two: true, three: 3 });
interface AClass { one: string; two: boolean; three?: number; } class aClass implements AClass { public one: string = 'one'; public two: boolean = true; private four: string = 'four'; }
interface IOldClass { zero: number; } interface INewClass extends IOldClass { one: string; two: boolean; three?: number; } class NewClass implements INewClass { zero: number = 0; one: string = 'one'; two: boolean = true; }
interface IHybridClass { one: string; two: { twoOne: boolean; }; three(threeOne: string, threeTwo: boolean): number; four({ fourOne, fourTwo }: { fourOne: string, fourTwo: boolean }): number; } class HybridClass implements IHybridClass { one = 'one'; two = { twoOne: true }; three = (threeOne: string, threeTwo: boolean) => { return 3; }; testingThree() { let three = this.three('one', true); } four = function ({ fourOne, fourTwo }: { fourOne: string, fourTwo: boolean }) { console.log(fourOne, fourTwo); return 4; } testingFour() { let four = this.four({ fourOne: 'four', fourTwo: true }); } }
function identity<T>(arg: T): T { return arg; } identity(10); identity<number>(10); identity('ten'); identity<string>('ten');
interface ILoggingIdentity2 { length: number; } function loggingIdentity2<T extends ILoggingIdentity2>(arg: T): T { console.log(arg.length); return arg; } loggingIdentity2({ length: 1 });
enum TodoState { New = 1, Active, Complete, Deleted }
Literal Union | 'one' | 'two' |
Type Union | boolean | string |
any | avoid it |
unknown | avoid it |
never | when a function returns an error |
Omit | Opposite of Pick interface Todo { one: string; two: string; } const todo: Omit<Todo, "one"> = { two: "Two" } |
Pick | Opposite of Omit interface Todo { one: string; two: string; } const todo: Omit<Todo, "one"> = { one: "One" } |
Partial | All properties are optional interface Todo { one: string; two: string; } const todo: Partial<Todo> = {} |
interface IButton { text: string; color?: string; size?: number; } function Button({ text, color, size }: IButton) { // variables // logic // return <button>{text}</button>; } export default Button; <Button text="Click Here" /> <Button text="Click Here" color="purple" size={10} />