Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript definition for clearFlags and setFlags don't allow bitwise operations. #347

Open
abuiles opened this issue May 13, 2020 · 1 comment
Labels

Comments

@abuiles
Copy link
Contributor

abuiles commented May 13, 2020

You can use bitwise operation to turn on multiple flags in the same transaction, however, TypeScript complains that the value isn't valid, since it's expecting the value to be either 1,2, or 4.

Operation.setOptions({
    setFlags: AuthRequiredFlag | AuthRevocableFlag
})

It's very likely other fields with bitwise support have the same issue.

@abuiles abuiles added the bug label May 13, 2020
@silence48
Copy link
Contributor

Hello, this issue caught my eye as i had some problems with eslints bitwise in another place so i got looking to see what would cause it. Well it seems you can't use a bitmap value with typescript or at least i couldn't figure out how. so first i made a list of what the other numbers that are valid inputs would be, however keep in mind there would be other places these types are used that would also need to be updated. it seems there should some way to do this.


export const AuthRevocableFlag: 2;  //                 [0 1 0 0]
export const RequiredRevocable: 3;   // 1+2            [1 1 0 0]

export const AuthImmutableFlag: 4; //                  [0 0 1 0]
export const RequiredImmutable: 5; // 1+4              [1 0 1 0]
export const RevocableImmutable: 6; // 4+2             [0 1 1 0]
export const RequiredRevocableImmutable: 7;// 1+2      [1 1 1 0]

export const AuthClawbackEnabledFlag: 8;//              [0 0 0 1]
export const RequiredClawback: 9; //   8+1              [1 0 0 1]
export const RevocableClawback: 10; //  2+8             [0 1 0 1]
export const RequiredRevocableClawback: 11; // 1+2+8    [1 1 0 1]
export const RequiredImmutableClawback: 14; //1+5+8     [1 0 1 1]
export const ImmutableClawback: 12; // 4+8              [0 0 1 1]
export const RevocableImmutableClawback: 13; //         [0 1 1 1]
export const RequiredRevocableImmutableClawback: 15; // [1 1 1 1]

export namespace AuthFlag {
  type required = typeof AuthRequiredFlag;
  type revocable = typeof AuthRevocableFlag;
  type requiredRevocable = typeof RequiredRevocable;
  
  type immutable = typeof AuthImmutableFlag;
  type requiredImmutable = typeof RequiredImmutable;
  type revocableImmutable = typeof RevocableImmutable;
  type requiredRevocableImmutable = typeof RequiredRevocableImmutable;
  
  type clawbackEnabled = typeof AuthClawbackEnabledFlag;
  type requiredClawback = typeof RequiredClawback;
  type revocableClawback = typeof RevocableClawback;
  type requiredRevocableClawback = typeof RequiredRevocableClawback;
  type requiredImmutableClawback = typeof RequiredImmutableClawback;
  type immutableClawback = typeof ImmutableClawback;
  type revocableImmutableClawback = typeof RevocableImmutableClawback;
  type requiredRevocableImmutableClawback = typeof RequiredRevocableImmutableClawback;
}

export type AuthFlag =
  | AuthFlag.required
  | AuthFlag.revocable
  | AuthFlag.requiredRevocable
  | AuthFlag.immutable
  | AuthFlag.requiredImmutable
  | AuthFlag.revocableImmutable
  | AuthFlag.requiredRevocableImmutable
  | AuthFlag.clawbackEnabled
  | AuthFlag.requiredClawback
  | AuthFlag.revocableClawback
  | AuthFlag.requiredRevocableClawback
  | AuthFlag.requiredImmutableClawback
  | AuthFlag.immutableClawback
  | AuthFlag.revocableImmutableClawback
  | AuthFlag.requiredRevocableImmutableClawback;

Or maybe something like this (but this might not work in a d.ts file)

export enum AuthFlags {
  AuthRequiredFlag = 0x1,
  AuthRevocableFlag = 0x2,
  AuthImmutableFlag = 0x4,
  AuthClawbackEnabledFlag = 0x8,
}

export type AuthFlag = {
  [P in keyof typeof AuthFlags]?: boolean;
};```

I'm not sure the best way to solve it but i found this an amusing diversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants