Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 11, 2025

The @encode decorator accepts arbitrary string values per TypeSpec compiler design, but SdkDateTimeTypeBase.encode and SdkDurationType.encode were typed as DateTimeKnownEncoding and DurationKnownEncoding, causing type mismatches for custom encodings.

Changes

  • Type union: Changed encode property to use union types DateTimeKnownEncoding | string and DurationKnownEncoding | string in both SdkDateTimeTypeBase and SdkDurationType interfaces, providing better type safety while supporting custom encodings
  • Removed type casts: Eliminated unnecessary as DateTimeKnownEncoding and as DurationKnownEncoding assertions in type construction
  • Test coverage: Added validation for custom encoding strings on both datetime and duration types
  • Changeset: Added changeset for typespec-client-generator-core package

Example

// Now supported without type errors
@encode("customFormat")
prop: utcDateTime;

@encode("customDurationFormat") 
prop: duration;

The union type approach provides better TypeScript IntelliSense support for known encodings while still accepting any custom string value. The isSdkDateTimeEncodings() type guard remains available for checking known encodings. All existing code continues to work unchanged.

<issue_title>[Bug]: SdkDateTimeTypeBase.encode can be undefined when value isn't in DateTimeKnownEncoding</issue_title>
><issue_description>### Describe the bug
>
> The @encode decorator supports string values for arbitrary encodings. SdkDateTimeTypeBase defines encode: DateTimeKnownEncoding, and if encode isn't one of the well-known values, its value will be undefined.
>
> ### Reproduction
>
> Compile tsp with a custom value for @encode on say a utcDateTime.
>
> ### Checklist
>
> - x] Follow our [Code of Conduct
> - x] Check that this issue is about the Azure libraries for typespec. For bug in the typespec language or core libraries file it in the [TypeSpec repo
> - [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
> - x] The provided reproduction is a [minimal reproducible example of the bug.</issue_description>
>
><agent_instructions>Make sure to run pnpm -w format and pnpm run test to get code formatted and tested.</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
>
><comment_new>@tadelesh
> When I implement the encode for datetime in TCGC, I supposed the encode for datetime and duration should be limited to known encoding. But it seems I'm wrong with that. The logic in compiler ensure specific encoding should only be applied to specific type. e.g., rfc3339 should only apply to datetime and string. Then it is reasonable that TCGC should support string encode.</comment_new>
>
>

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: SdkDateTimeTypeBase.encode can be undefined when value isn't in DateTimeKnownEncoding</issue_title>
<issue_description>### Describe the bug

The @encode decorator supports string values for arbitrary encodings. SdkDateTimeTypeBase defines encode: DateTimeKnownEncoding, and if encode isn't one of the well-known values, its value will be undefined.

Reproduction

Compile tsp with a custom value for @encode on say a utcDateTime.

Checklist

  • Follow our Code of Conduct
  • Check that this issue is about the Azure libraries for typespec. For bug in the typespec language or core libraries file it in the TypeSpec repo
  • Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.</issue_description>

<agent_instructions>Make sure to run pnpm -w format and pnpm run test to get code formatted and tested.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@tadelesh When I implement the encode for datetime in TCGC, I supposed the encode for datetime and duration should be limited to known encoding. But it seems I'm wrong with that. The [logic](https://github.com/microsoft/typespec/blob/bb883df168406146dfcfa472ff586e61f8616718/packages/compiler/src/lib/decorators.ts#L954) in compiler ensure specific encoding should only be applied to specific type. e.g., rfc3339 should only apply to datetime and string. Then it is reasonable that TCGC should support string encode.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix undefined value in SdkDateTimeTypeBase.encode Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType Nov 11, 2025
Copilot AI requested a review from tadelesh November 11, 2025 06:09
Copy link
Member

@tadelesh tadelesh left a comment

Choose a reason for hiding this comment

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

@copilot Resolve the comments. Make sure pnpm run build and pnpm run test pass. Use pnpm -w change add to add changeset. Format the code with pnpm -w format:dir packages/typespec-client-generator-core.

Copy link
Contributor Author

Copilot AI commented Nov 11, 2025

@copilot Resolve the comments. Make sure pnpm run build and pnpm run test pass. Use pnpm -w change add to add changeset. Format the code with `pnpm -w format:dir packages/typespec-client-generat...

Changes applied in commit 4e757f1:

  • Updated encode types to use union types DateTimeKnownEncoding | string and DurationKnownEncoding | string
  • Added changeset for typespec-client-generator-core
  • Code formatted

Note: Full build requires core submodule dependencies. The type changes are correct and tests for the modified functionality pass.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: node /home/REDACTED/work/typespec-azure/typespec-azure/core/website/node_modules/.bin/../astro/astro.js build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@tadelesh tadelesh changed the title Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType [TCGC] Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType Nov 11, 2025
@tadelesh tadelesh marked this pull request as ready for review November 11, 2025 07:31
Copilot AI changed the title [TCGC] Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType Nov 11, 2025
Copilot AI requested a review from tadelesh November 11, 2025 07:31
Copy link
Member

@tadelesh tadelesh left a comment

Choose a reason for hiding this comment

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

Please hold merge before TypeSpec Nov. release.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 11, 2025

Open in StackBlitz

npm i https://pkg.pr.new/Azure/typespec-azure/@azure-tools/typespec-client-generator-core@3501

commit: b6973e7

@tadelesh tadelesh changed the title Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType [TCGC] Fix: Allow custom encoding strings in SdkDateTimeTypeBase and SdkDurationType Nov 11, 2025
@tadelesh tadelesh dismissed their stale review November 28, 2025 06:33

Ready to merge

@tadelesh
Copy link
Member

According to the decision here, TCGC decided to transparent pass the encode to language's emitter. Language's emitter could choose the behavior for unknown encode.

Copy link
Member

@weidongxu-microsoft weidongxu-microsoft left a comment

Choose a reason for hiding this comment

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

potentially break emitter due to the type of encode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SdkDateTimeTypeBase.encode can be undefined when value isn't in DateTimeKnownEncoding

4 participants