Q:

Call method from class

I want to call a method from a class.  I tried this:
export class IdGenerator {

  public randomString(): string {
    const length = 40;
    const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let result = '';
    for (let i = length; i > 0; --i) {
      result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
  }
}

I call it here:
import {IdGenerator} from '@utils/id.generator';

@Component({
  selector: 'app-merchant-new',
  templateUrl: './merchant-new.component.html',
  styleUrls: ['./merchant-new.component.scss']
})
export class MerchantNewComponent extends FormBaseComponent {

  formGroup = new FormGroup({
    login: new FormControl(IdGenerator.randomString(), [Validators.required, Validators.maxLength(40)]),
  });

  constructor(private merchantService: MerchantService,
              private router: Router) {
    super();
  }
}

But I get error TS2339: Property 'randomString' does not exist on type 'typeof IdGenerator'. 
What is the proper way to call method and get String? 

A:

You have defined randomString as instance method.  So, you have to call it as 
new IdGenerator().randomString()

If you want to call it as IdGenerator.randomString() then you should define this method as static: 
export class IdGenerator {

  static randomString(): string {
    ...
  }
}

See working example here

