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

Close #848 - exactOptionalPropertyTypes option for typia.protobuf.decode<T>() function. #874

Merged
merged 1 commit into from
Nov 19, 2023

Conversation

samchon
Copy link
Owner

@samchon samchon commented Nov 19, 2023

If user defines exactOptionalPropertyTypes option of tsconfig.json, typia will not generate a code that assigning undefined initial value to the optional property type.

TypeScript Source File

interface Something {
  id: string;
  not_required: string | undefined;
  optional?: string;
  optional_and_not_required?: string | undefined;
}

const decode = typia.protobuf.createDecode<Something>();

Compiled JavaScript File

(input) => {
  const $Reader = typia_1.default.protobuf.createDecode.Reader;
  const $pdo0 = (reader, length = -1) => {
    length = length < 0 ? reader.size() : reader.index() + length;
    const output = {
      id: "",
      not_required: undefined,
    };
    while (reader.index() < length) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          // string;
          output.id = reader.string();
          break;
        case 2:
          // string;
          output.not_required = reader.string();
          break;
        case 3:
          // string;
          output.optional = reader.string();
          break;
        case 4:
          // string;
          output.optional_and_not_required = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return output;
  };
  const reader = new $Reader(input);
  return $pdo0(reader);
};

…decode<T>()` function.

If user defines `exactOptionalPropertyTypes` option of `tsconfig.json`, `typia` will not generate a code that assigning `undefined` initial value to the `optional` property type.

## TypeScript Source File
```typescript
interface Something {
  id: string;
  not_required: string | undefined;
  optional?: string;
  optional_and_not_required?: string | undefined;
}

const decode = typia.protobuf.createDecode<Something>();
```

## Compiled JavaScript File
```javascript
(input) => {
  const $Reader = typia_1.default.protobuf.createDecode.Reader;
  const $pdo0 = (reader, length = -1) => {
    length = length < 0 ? reader.size() : reader.index() + length;
    const output = {
      id: "",
      not_required: undefined,
    };
    while (reader.index() < length) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          // string;
          output.id = reader.string();
          break;
        case 2:
          // string;
          output.not_required = reader.string();
          break;
        case 3:
          // string;
          output.optional = reader.string();
          break;
        case 4:
          // string;
          output.optional_and_not_required = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return output;
  };
  const reader = new $Reader(input);
  return $pdo0(reader);
};
```
@samchon samchon added the enhancement New feature or request label Nov 19, 2023
@samchon samchon self-assigned this Nov 19, 2023
Copy link
Owner Author

@samchon samchon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem in the test automation program

@samchon samchon merged commit ab39b05 into v5.3 Nov 19, 2023
3 checks passed
@samchon samchon deleted the features/exactOptionalPropertyTypes branch November 19, 2023 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant