Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface IUser {
- username: string;
- pass: string;
- id: number;
- }
- type IPartial<TObject> = {
- [property in keyof TObject]?: TObject[property];
- }
- class User<TParams> {
- constructor(private params: IPartial<TParams>) {
- }
- public set<TKey extends keyof TParams>(key: TKey, value: TParams[TKey]): void;
- public set(params: IPartial<TParams>): void;
- public set(key? :any, value?: any): void {
- if (typeof key === 'string') {
- this.params[key as keyof TParams] = value;
- return;
- }
- else if (typeof key === 'object') {
- const params = key as IPartial<TParams>;
- for (let paramKey in params) {
- this.set(paramKey, params[paramKey]);
- }
- return;
- }
- else {
- throw new TypeError('func up');
- }
- }
- public get<TKey extends keyof TParams>(key: TKey): TParams[TKey] {
- return this.params[key];
- }
- }
- const usr = new User<IUser>({
- pass: '123'
- });
- usr.set('pass', '123');
- usr.set('id', 1);
- usr.set({
- pass: 'google'
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement