1. Let's build a Teacher interfacemandatoryCreate a directory task_1 and copy these configuration files into this folder: package.json, tsconfig.json, webpack.config.jsfirstName(string) and lastName(string). These two attributes should only be modifiable when a Teacher is first initializedfullTimeEmployee(boolean) this attribute should always be definedyearsOfExperience(number) this attribute is optionallocation(string) this attribute should always be definedAdd the possibility to add any attribute to the Object like contract(boolean) without specifying the name of the attributeExample:const teacher3: Teacher = { firstName: 'John', fullTimeEmployee: false, lastName: 'Doe', location: 'London', contract: false,};console.log(teacher3);// should print// Object// contract: false// firstName: "John"// fullTimeEmployee: false// lastName: "Doe"// location: "London"
Question
- Let's build a Teacher interfacemandatoryCreate a directory task_1 and copy these configuration files into this folder: package.json, tsconfig.json, webpack.config.jsfirstName(string) and lastName(string). These two attributes should only be modifiable when a Teacher is first initializedfullTimeEmployee(boolean) this attribute should always be definedyearsOfExperience(number) this attribute is optionallocation(string) this attribute should always be definedAdd the possibility to add any attribute to the Object like contract(boolean) without specifying the name of the attributeExample:const teacher3: Teacher = { firstName: 'John', fullTimeEmployee: false, lastName: 'Doe', location: 'London', contract: false,};console.log(teacher3);// should print// Object// contract: false// firstName: "John"// fullTimeEmployee: false// lastName: "Doe"// location: "London"
Solution
To create a Teacher interface in TypeScript, you can follow these steps:
-
Create a new directory named
task_1and copy the configuration files (package.json,tsconfig.json,webpack.config.js) into this directory. -
Open your preferred code editor and create a new TypeScript file in the
task_1directory. You can name itteacher.ts. -
In the
teacher.tsfile, define the Teacher interface. The interface should have the following properties:firstName,lastName,fullTimeEmployee,yearsOfExperience, andlocation. ThefirstNameandlastNameproperties should be of typestringand should only be modifiable when a Teacher object is first initialized. ThefullTimeEmployeeproperty should be of typebooleanand should always be defined. TheyearsOfExperienceproperty should be of typenumberand should be optional. Thelocationproperty should be of typestringand should always be defined.
Here is how you can define the Teacher interface:
interface Teacher {
firstName: string;
lastName: string;
fullTimeEmployee: boolean;
yearsOfExperience?: number;
location: string;
[propName: string]: any;
}
The [propName: string]: any; line allows you to add any additional properties to the Teacher object without specifying their names.
- Now, you can create a new Teacher object and log it to the console:
const teacher3: Teacher = {
firstName: 'John',
fullTimeEmployee: false,
lastName: 'Doe',
location: 'London',
contract: false,
};
console.log(teacher3);
When you run this code, it should print the following output:
Object
contract: false
firstName: "John"
fullTimeEmployee: false
lastName: "Doe"
location: "London"
Similar Questions
4. Writing a classmandatoryWrite a Class named StudentClass:The constructor accepts firstName(string) and lastName(string) argumentsThe class has a method named workOnHomework that return the string Currently workingThe class has a method named displayName. It returns the firstName of the studentThe constructor of the class should be described through an InterfaceThe class should be described through an InterfaceRequirements:You can reuse the Webpack configuration from the previous exercise to compile the code.When running npm run build, no TypeScript error should be displayed.Every variable should use TypeScript when possible.
2. Extending the Teacher classmandatoryWrite an interface named Directors that extends Teacher. It requires an attribute named numberOfReports(number)Example:const director1: Directors = { firstName: 'John', lastName: 'Doe', location: 'London', fullTimeEmployee: true, numberOfReports: 17,};console.log(director1);// should print// Object// firstName: "John"// fullTimeEmployee: true// lastName: "Doe"// location: "London"// numberOfReports: 17
2. A Course, Getters, and SettersmandatoryImplement a class named HolbertonCourse:Constructor attributes:name (String)length (Number)students (array of Strings)Make sure to verify the type of attributes during object creationEach attribute must be stored in an “underscore” attribute version (ex: name is stored in _name)Implement a getter and setter for each attribute.bob@dylan:~$ cat 2-main.jsimport HolbertonCourse from "./2-hbtn_course.js";const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"])console.log(c1.name);c1.name = "Python 101";console.log(c1);try { c1.name = 12;} catch(err) { console.log(err);}try { const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);}catch(err) { console.log(err);}bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js ES6HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ]}TypeError: Name must be a string ...TypeError: Length must be a number ...bob@dylan:~$
A module consists of, exept ?AfunctionsBpackagesCvariablesDclasses
5. Advanced types Part 1mandatoryCreate the DirectorInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkDirectorTasks() returning a stringCreate the TeacherInterface interface with the 3 expected methods:workFromHome() returning a stringgetCoffeeBreak() returning a stringworkTeacherTasks() returning a stringCreate a class Director that will implement DirectorInterfaceworkFromHome should return the string Working from homegetToWork should return the string Getting a coffee breakworkDirectorTasks should return the string Getting to director tasksCreate a class Teacher that will implement TeacherInterfaceworkFromHome should return the string Cannot work from homegetCoffeeBreak should return the string Cannot have a breakworkTeacherTasks should return the string Getting to workCreate a function createEmployee with the following requirements:It can return either a Director or a Teacher instanceIt accepts 1 arguments:salary(either number or string)if salary is a number and less than 500 - It should return a new Teacher. Otherwise it should return a DirectorExpected result:console.log(createEmployee(200));Teacherconsole.log(createEmployee(1000));Directorconsole.log(createEmployee('$500'));Director
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.