publicmove(distanceMeter: number = 0) { console.log(`${this.name} moved ${distanceMeter}m`); } }
newAnimal('small cat').name;
运行后得到如下结果
1 2 3
$ npx ts-node src/classes_3.ts ⨯ Unable to compile TypeScript: src/classes_3.ts(12,25): error TS2341: Property 'name' is private and only accessible within class 'Animal'.
publicmove(distanceMeter: number = 0) { console.log(`${this.name} moved ${distanceMeter}m`); } }
let animal = newAnimal('animal'); let dog = newDog('dog'); let person = newPerson('person');
animal = dog animal = person;
运行后得到如下结果
1 2 3 4
$ npx ts-node src/classes_3.ts ⨯ Unable to compile TypeScript: src/classes_3.ts(35,1): error TS2322: Type 'Person' is not assignable to type'Animal'. Types have separate declarations of a private property 'name'.
let aEmployee = newEmployee('durban', '华盛顿'); console.log(aEmployee.getWorkInfo()); console.log(aEmployee.name);
运行后得到的结果如下
1 2 3
$ npx ts-node src/classes_3.ts ⨯ Unable to compile TypeScript: src/classes_3.ts(23,23): error TS2445: Property 'name' is protected and only accessible within class 'Person' and its subclasses.
let aEmployee = newEmployee('durban', '华盛顿'); let aPerson = newPerson('Sakuro');
运行后得到如下错误
1 2 3
$ npx ts-node src/classes_3.ts ⨯ Unable to compile TypeScript: src/classes_3.ts(22,15): error TS2674: Constructor of class 'Person' is protected and only accessible within the class declaration.
$ npx ts-node src/classes_3.ts ⨯ Unable to compile TypeScript: src/classes_3.ts(9,9): error TS2540: Cannot assign to 'name' because it is a constant or a read-only property.
⨯ Unable to compile TypeScript: src/interface_7.ts(20,7): error TS2420: Class 'SomeClass' incorrectly implements interface 'SomeConstructor'. Type 'SomeClass' provides no match for the signature 'new (arg1: string, arg2: string): any'.
functioncreateSquare(config: SquareConfig): { color: string; area: number } { let newSquare = { color: "white", area: 100 }; if (config.color) { newSquare.color = config.color; } if (config.width) { newSquare.area = config.width * config.width; } return newSquare; }
let mySquare = createSquare({ colour: "red", width: 100 });
运行后接到的结果如下
1 2 3
⨯ Unable to compile TypeScript: src/interface_4.ts(17,31): error TS2345: Argument of type'{ colour: string; width: number; }' is not assignable to parameter of type'SquareConfig'. Object literal may only specify known properties, but 'colour' does not exist intype'SquareConfig'. Did you mean to write 'color'?
⨯ Unable to compile TypeScript: src/interface_3.ts(11,1): error TS2542: Index signature intype'ReadonlyArray<number>' only permits reading. src/interface_3.ts(12,4): error TS2339: Property 'push' does not exist on type'ReadonlyArray<number>'. src/interface_3.ts(13,4): error TS2540: Cannot assign to 'length' because it is a constant or a read-only property. src/interface_3.ts(14,1): error TS2322: Type 'ReadonlyArray<number>' is not assignable to type'number[]'. Property 'push' is missing intype'ReadonlyArray<number>'.
functioncreateSquare(config: SquareConfig): { color: string; area: number } { let newSquare = {color: "white", area: 100}; if (config.clor) { // Error: Property 'clor' does not exist on type 'SquareConfig' newSquare.color = config.clor; } if (config.width) { newSquare.area = config.width * config.width; } return newSquare; }
let mySquare = createSquare({color: "black"}); console.log(mySquare);
运行后结果如下
1 2 3
⨯ Unable to compile TypeScript: src/interface_2.ts(27,16): error TS2551: Property 'clor' does not exist on type'SquareConfig'. Did you mean 'color'? src/interface_2.ts(29,34): error TS2551: Property 'clor' does not exist on type'SquareConfig'. Did you mean 'color'?