Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

typescript - What is the best way to manage static keys? (TS)

In my app I have always the issue to use some keys which are predefined and won't change at all - i.e. they are hardcoded. And I have no real clue which of the options are the best.

Currently I have:

export class PermissionKeys {
  public static readonly MYKEY_NOT_ALL_SPECIFIED = 'MYKEY_NOT_ALL_SPECIFIED';
}

as well as

export enum MyKey {
  MYKEY_NOT_ALL_SPECIFIED = <any>'MYKEY_NOT_ALL_SPECIFIED',
}

and in the final code I want to compare something like this.checkKey=MYKEY_NOT_ALL_SPECIFIED. With the enum I need to append always the .toString(). With the readonly strings I need to specify the name and the content (which remains always exactly the same).

Anybody have better ideas to manage hardcoded keys?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Without more information on your use case, I'm not sure what's the best way for you to go. You can use string enums:

export enum MyKey {
  MYKEY_NOT_ALL_SPECIFIED = 'MYKEY_NOT_ALL_SPECIFIED', // no <any>
  OTHERKEY = 'OTHERKEY',
  ETC = 'ETC'
}

class Something {
  checkKey: MyKey;
  someMethod() {
    if (this.checkKey === MyKey.MYKEY_NOT_ALL_SPECIFIED) {
      // do something
    } 
  }
}

Or, if you don't want to repeat yourself and know that the values and keys will always be equal, you can just use a constant plain object:

function sameValuesAsKeys<K extends string>(...values: K[]): {readonly [P in K]: P} {
  const ret = {} as {[P in K]: P}
  values.forEach(k => ret[k] = k);
  return ret;
}

export const MyKey = sameValuesAsKeys('MYKEY_NOT_ALL_SPECIFIED', "OTHERKEY", "ETC");
type MyKey = keyof typeof MyKey;

// still works
class Something {
  checkKey: MyKey;
  someMethod() {
    if (this.checkKey === MyKey.MYKEY_NOT_ALL_SPECIFIED) {
      // do something
    } 
  }
}

Does that answer your question? If not, please be more specific about your use case... show some code where you can't get the key you want or where you feel like you're repeating yourself, and I'll update the answer.

Hope that helps; good luck!


Update 1

Explaining the signature of

function sameValuesAsKeys<K extends string>(...values: K[]): {readonly [P in K]: P}

The type parameter K is some string or union of strings, and the parameter values is an array of that type of string. If I call sameValuesAsKeys("dog","cat"), TypeScript infers K to be "dog"|"cat". The return type of sameValuesAsKeys() is {readonly [P in K]: P} which is a mapped type meaning roughly "an object where each property is readonly and where the value is the same as the key". So if I call sameValuesAsKeys("dog","cat"), the return value is of type {readonly dog: "dog"; readonly cat: "cat"}.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...