private
and protected
members, we treat these types differently. For two types to be considered compatible, if one of the them has a private
member, then the other must have a private
member that originated in the same declaration. The same applies to protected
members.protected
modifier acts much like the private
modifier with the exception that members declared protected
can also be accessed within deriving classes.protected
. This means that the class cannot be instantiated outside of its containing class, but can be extended.abstract
keyword i used to define abstract classes as well as abstract methods within an abstract class.class Animal { private name: string; constructor( theName: string ) { this.name = theName; } } class Rhino extends Animal { constructor() { super( 'Rhino' ); } } class Employee { private name: string; constructor( theName: string ) { this.name = theName; } } let animal = new Animal( 'Goat' ); let rhino = new Rhino(); let employee = new Employee( 'Bob' ); animal = rhino; // Type 'Employee' is not assignable to type 'Animal'. // Types have separate declarations of a private property 'name'. animal = employee;
class Octopus { readonly options: Record<string, string>; constructor() { this.options = {} } } let dad = new Octopus(); dad.options.name = 'Achilles';
class Octopus { constructor( readonly name: string, private age?: number ){} } new Octopus( 'Achilles' ); new Octopus( 'Achilles', 30 );
abstract class Animal { abstract makeSound(): void; move(): void { console.log( 'roaming the earth...' ); } }
import Controller from './controller'; export type YnnOptions = { controllers?: Record<string, typeof Controller>; } export default class Ynn extends Koa { controllers: Record<string, typeof Controller>; constructor( options: YnnOptions = {} ) { super(); this.controllers = options.controllers || {}; } }
class Controller {} class Provider { constructor( public options: Record<string, any> ) {} } type YnnOptions = { controllers: Record<string, typeof Controller>; providers: Record<string, { new(): any }>; } const options: YnnOptions = { controllers : { index : Controller, }, providers : { // Type 'typeof Provider' is not assignable to type 'new () => any'. user : Provider } }
class Point { x: number; y: number; } interface Point3d extends Point { z: number; } let point3d: Point3d = { x : 1, y : 2, z : 3 };
function Bird() { } Bird.prototype.fly = function() { } // 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.(7009) const bird = new Bird();
declare class Bird { fly: () => void; } function Bird() { } Bird.prototype.fly = function() { } const bird = new Bird();
function Bird() { } Bird.prototype.fly = function() { } const bird = new ( Bird as unknown as { new (): any } )();