Javascript 是否可以在TypeScript中定义属性名称的类型
因此,我有一个常量Javascript 是否可以在TypeScript中定义属性名称的类型,javascript,typescript,Javascript,Typescript,因此,我有一个常量类,它定义了保存到localStorage/chrome.storage的变量的名称,以及如果存储中不存在这些项,它们的默认值是什么 设置Constants.ts export class SettingsConstants { public static SETTINGS_ALLOWED: string = "settingsAllowed"; public static QUIT_ALLOWED: string = "quitAllowed"; public
类
,它定义了保存到localStorage
/chrome.storage
的变量的名称,以及如果存储中不存在这些项,它们的默认值是什么
设置Constants.ts
export class SettingsConstants {
public static SETTINGS_ALLOWED: string = "settingsAllowed";
public static QUIT_ALLOWED: string = "quitAllowed";
public static DEFAULTS: any = () => {
const defaults = {};
defaults[SettingsConstant.SETTINGS_ALLOWED] = true;
defaults[SettingsConstant.QUIT_ALLOWED] = true;
return defaults;
};
public static STORAGE_STRINGS: string[] = [
SettingsConstant.SETTINGS_ALLOWED,
SettingsConstant.QUIT_ALLOWED
]
}
export class SettingsService implements ISettingsService {
private settingsAllowed: boolean = undefined;
private quitAllowed: boolean = undefined;
constructor(logger: Logger) {
this.logger = logger;
this.getItemsFromStorage();
}
private getItemsFromStorage(): void {
const storageStrings: string[] = SettingsConstant.STORAGE_STRINGS;
for (let i = 0; i < storageStrings.length; i++) {
const currentString = storageStrings[i];
if (!this.hasOwnProperty(currentString)) {
throw "The property is not defined, " +
"check if it matches the STORAGE_STRING in SettingsConstants";
}
const currentItem = this.getItem(currentString);
this[currentString] = currentItem === undefined
? SettingsConstant.DEFAULTS()[currentString]
: currentItem;
this.logger.log(currentString, "=", this[currentString]);
}
}
private getItem(item: any): any {
const localItem: string = localStorage.getItem(item);
if (localItem === undefined || localItem === null || localItem === 'undefined') {
return undefined;
} else {
return JSON.parse(localItem);
}
}
}
然后我有了SettingsService
,它在getItemsFromStorage()
方法中使用这些常量。基本上,它所做的是在设置约束
中定义的字符串中循环,并将其自身的属性
设置为等于它从存储
中获得的值,否则设置默认的值
设置服务ts
export class SettingsConstants {
public static SETTINGS_ALLOWED: string = "settingsAllowed";
public static QUIT_ALLOWED: string = "quitAllowed";
public static DEFAULTS: any = () => {
const defaults = {};
defaults[SettingsConstant.SETTINGS_ALLOWED] = true;
defaults[SettingsConstant.QUIT_ALLOWED] = true;
return defaults;
};
public static STORAGE_STRINGS: string[] = [
SettingsConstant.SETTINGS_ALLOWED,
SettingsConstant.QUIT_ALLOWED
]
}
export class SettingsService implements ISettingsService {
private settingsAllowed: boolean = undefined;
private quitAllowed: boolean = undefined;
constructor(logger: Logger) {
this.logger = logger;
this.getItemsFromStorage();
}
private getItemsFromStorage(): void {
const storageStrings: string[] = SettingsConstant.STORAGE_STRINGS;
for (let i = 0; i < storageStrings.length; i++) {
const currentString = storageStrings[i];
if (!this.hasOwnProperty(currentString)) {
throw "The property is not defined, " +
"check if it matches the STORAGE_STRING in SettingsConstants";
}
const currentItem = this.getItem(currentString);
this[currentString] = currentItem === undefined
? SettingsConstant.DEFAULTS()[currentString]
: currentItem;
this.logger.log(currentString, "=", this[currentString]);
}
}
private getItem(item: any): any {
const localItem: string = localStorage.getItem(item);
if (localItem === undefined || localItem === null || localItem === 'undefined') {
return undefined;
} else {
return JSON.parse(localItem);
}
}
}
要检查类
中是否存在该变量,如果不存在,则意味着我们可能拼写错误,或者忘记添加它
我可以通过将它们添加到ISettingsService
接口
来解决此问题,但这意味着属性
将变为公共
,这不是我的本意
我想要的解决方案
我想定义一个类型,在这里我可以定义允许使用的变量的名称。与此类似
export declare namespace SettingsTypes{
type SettingsType = "settingsAllowed" | "quitAllowed";
}
然后在设置服务中,我可以编写以下内容
private settingsAllowed: SettingsTypes.SettingType: boolean;
因此,其类似的settingsallow
name必须是SettingsTypes.SettingType
类型,其返回类型为boolean
然而,这似乎没有多大意义,而且似乎没有办法正确地做到这一点,所以我想知道你们中的任何一位聪明人是否有解决这个问题的好办法。这是一个有趣的问题。我去看了一些文件。请特别查看类型防护装置的部分。也许你可以做一些类似的事情,比如为布尔值做一个类型保护
function isSetting(setting: any): setting is Settings {
return typeof setting === SettingsTypes.SettingType;
}
类似的东西。这只是一个可能的想法
它仅适用于函数,不适用于属性名称。确定。我明白了。它是否仍然简化了代码?我需要检查属性的名称,并检查它是否为SettingsType类型,如果是字符串或数字等简单类型,则检查属性名称并不是那么简单。我想我会把它们定义为未定义的。它并不漂亮,但似乎是最好的解决方案有时你需要用最好的解决方案。并非总是可以按照惯例把它变得漂亮漂亮:)