Skip to content

Instantly share code, notes, and snippets.

@PranavBhatia
Created March 22, 2020 23:28
Show Gist options
  • Save PranavBhatia/0789c5379ff67a00324f4fcb93996b96 to your computer and use it in GitHub Desktop.
Save PranavBhatia/0789c5379ff67a00324f4fcb93996b96 to your computer and use it in GitHub Desktop.
// Interface can also be called a contract.
// We can use them to define custom types without creating classes.
// Interfaces ARE NOT compiled to JavaScript! It's just for checking/validation done by our TypeScript compiler.
// Employee interface
interface Employee {
employeeName: string;
salary: number;
mobileNumber?: string; // Optional property => Does not have to be implemented
}
let employee: Employee;
// This value does not satisfy the interface => Compilation error
// user = { anything: 'anything', anynumber: 5};
// This value does satisfy the interface
employee = {employeeName: 'John', salary: 1234};
console.log(employee);
// Result => { employeeName: 'John', salary: 1234 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment