Skip to content

Commit 1e6eea5

Browse files
authored
fix: use intersection for DateTime, DateTimeOffset type definitions (#373)
1 parent 2d9c155 commit 1e6eea5

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

docs/reference/dates.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
| C# Type | JS Type |
55
|------------------|--------------------------------------------------------|
6-
| `DateTime` | `Date \| { kind?: 'utc' \| 'local' \| 'unspecified' }` |
7-
| `DateTimeOffset` | `Date \| { offset?: number }` |
6+
| `DateTime` | `Date & { kind?: 'utc' \| 'local' \| 'unspecified' }` |
7+
| `DateTimeOffset` | `Date & { offset?: number }` |
88
| `TimeSpan` | `number` (milliseconds) |
99

1010
## JS Date / .NET DateTime & DateTimeOffset
@@ -30,7 +30,7 @@ interoperability, both types of .NET values are convertible to and from JS `Date
3030
accomplished by adding either a `kind` or `offset` property to a regular `Date` object.
3131

3232
```TypeScript
33-
type DateTime = Date | { kind?: 'utc' | 'local' | 'unspecified' }
33+
type DateTime = Date & { kind?: 'utc' | 'local' | 'unspecified' }
3434
```
3535
3636
When a .NET `DateTime` is marshalled to a JS `Date`, the date's UTC timestamp value becomes the
@@ -43,7 +43,7 @@ with `Utc` kind. (Defaulting to `Unspecified` would be more likely to result in
4343
conversions to/from local-time.)
4444
4545
```TypeScript
46-
type DateTimeOffset = Date | { offset?: number }
46+
type DateTimeOffset = Date & { offset?: number }
4747
```
4848
4949
When a .NET `DateTimeOffset` is marshalled to a JS `Date`, the UTC timestamp value _without the

src/NodeApi.Generator/TypeDefinitionsGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,18 +746,18 @@ interface IDisposable { dispose(): void; }
746746
if (_emitDateTimeOffset)
747747
{
748748
s.Insert(insertIndex, _isSystemAssembly ? @"
749-
declare namespace js { type DateTimeOffset = Date | { offset?: number } }
749+
declare namespace js { type DateTimeOffset = Date & { offset?: number } }
750750
" : @"
751-
type DateTimeOffset = Date | { offset?: number }
751+
type DateTimeOffset = Date & { offset?: number }
752752
");
753753
}
754754

755755
if (_emitDateTime)
756756
{
757757
s.Insert(insertIndex, _isSystemAssembly ? @"
758-
declare namespace js { type DateTime = Date | { kind?: 'utc' | 'local' | 'unspecified' } }
758+
declare namespace js { type DateTime = Date & { kind?: 'utc' | 'local' | 'unspecified' } }
759759
" : @"
760-
type DateTime = Date | { kind?: 'utc' | 'local' | 'unspecified' }
760+
type DateTime = Date & { kind?: 'utc' | 'local' | 'unspecified' }
761761
");
762762
}
763763
}

test/TestCases/napi-dotnet/complex_types.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ ComplexTypes.testEnum = enumType.Two;
8181
assert.strictEqual(ComplexTypes.testEnum, enumType.Two);
8282

8383
// DateTime
84-
/** @type {Date | { kind: 'utc' | 'local' | 'unspecified' }} */
84+
/** @type {Date & { kind: 'utc' | 'local' | 'unspecified' }} */
8585
const dateValue = ComplexTypes.dateTime;
8686
assert(dateValue instanceof Date);
8787
assert.strictEqual(dateValue.valueOf(), new Date('2023-04-05T06:07:08').valueOf());
8888
assert.strictEqual(dateValue.kind, 'unspecified');
8989
ComplexTypes.dateTime = new Date('2024-03-02T11:00');
9090
assert.strictEqual(ComplexTypes.dateTime.valueOf(), new Date('2024-03-02T11:00').valueOf());
9191
assert.strictEqual(ComplexTypes.dateTime.kind, 'utc');
92-
/** @type {Date | { kind: 'utc' | 'local' | 'unspecified' }} */
92+
/** @type {Date & { kind: 'utc' | 'local' | 'unspecified' }} */
9393
const dateValue2 = new Date('2024-03-02T11:00');
9494
dateValue2.kind = 'local';
9595
ComplexTypes.dateTime = dateValue2;
@@ -110,15 +110,15 @@ ComplexTypes.timeSpan = (2*24*60*60 + 23*60*60 + 34*60 + 45) * 1000;
110110
assert.strictEqual(ComplexTypes.timeSpan, (2*24*60*60 + 23*60*60 + 34*60 + 45) * 1000);
111111

112112
// DateTimeOffset
113-
/** @type {Date | { offset: number }} */
113+
/** @type {Date & { offset: number }} */
114114
const dateTimeOffsetValue = ComplexTypes.dateTimeOffset;
115115
assert(dateTimeOffsetValue instanceof Date);
116116
// A negative offset means the UTC time is later than the local time,
117117
// so the offset is added to the local time to get the expected UTC time here.
118118
assert.strictEqual(dateTimeOffsetValue.valueOf(), Date.UTC(2023, 3, 5, 6, 7, 8) + 90 * 60 * 1000);
119119
assert.strictEqual(dateTimeOffsetValue.offset, -90);
120120
assert.strictEqual(dateTimeOffsetValue.toString(), '2023-04-05 06:07:08.000 -01:30');
121-
/** @type {Date | { offset: number }} */
121+
/** @type {Date & { offset: number }} */
122122
const dateTimeOffsetValue2 = new Date(Date.UTC(2024, 2, 2, 1, 0, 0));
123123
dateTimeOffsetValue2.offset = 120;
124124
ComplexTypes.dateTimeOffset = dateTimeOffsetValue2;

0 commit comments

Comments
 (0)