Q: Validating constructor parameter and lint error I'm trying to validate a constructor parameter when creating an instance of a class.
The parameter should be an object containing exactly all the properties (of adequate type) as defined in the class.
If this is not the case, I'd like TypeScript to lint the mismatch.
class User {
    username: string;
    // more properties

    constructor(data:object) {
        // Check if data Obejct exactly all the class properties and they are of the right type;
        // Set instance properties
    }
};

// Desired Output
new User(); // "data parameter missing";
new User(45); // "data parameter is not of type object";
new User(); // "username Poperty missing!";
new User({username:"Michael"}); // Valid;
new User({username:43}); // "username is not of type string";
new User({username:"Michael", favoriteFood: "Pizza"}); // "favoriteFood is not a valid property";

tsconfig.json
{
  "compilerOptions": {
    "target": "es2016",
    "module": "es2015",
    "lib": [
      "es2016.array.include"
    ],
    "downlevelIteration": true,
    "strict": true
  }
}


A: The solution is declaring an interface :
interface UserProps {
  username: string;
}

class User implements UserProps {
  username: string;
  // more properties

  constructor (data: UserProps) {
    // Check if data Obejct exactly all the class properties and they are of the right type;
    // Set instance properties
  }
}

