到目前为止,我对 TypeScript 并不熟悉,但我还没有遇到过任何语言的这个,我在 ASP.NET Core + React-Redux + TS 样板中看到了这段代码。请解释为什么需要在声明的描述中声明一个相同类型的静态字段?
import { AxiosResponse } from 'axios';
import { BaseService } from './base.service';
import { IAuthUser, ICredentials } from '../store/auth/types';
/**
* Auth API abstraction layer communication via Axios (typescript singleton pattern)
*/
class AuthService extends BaseService {
private static _authService: AuthService; // вот эта строка
private static _controllerName: string = 'Auth';
private constructor(controllerName: string) {
super(controllerName);
}
public static get Instance(): AuthService {
return this._authService || (this._authService = new this(this._controllerName));
}
public async logoutAsync(): Promise<AxiosResponse> {
return await this.$http.post('Logout');
}
public async loginAsync(credentials: ICredentials): Promise<IAuthUser> {
const { data } = await this.$http.post<IAuthUser>('Login', credentials);
return data;
}
}
export const AuthApi = AuthService.Instance;
一块从这里