Skip to content

Commit 68a9ba2

Browse files
committed
use metric units without custom and none units
1 parent 9384290 commit 68a9ba2

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

packages/core/src/attributes.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEBUG_BUILD } from './debug-build';
2-
import type { MeasurementUnit } from './types-hoist/measurement';
2+
import type { DurationUnit, FractionUnit, InformationUnit, MeasurementUnit, NoneUnit } from './types-hoist/measurement';
33
import { debug } from './utils/debug-logger';
44

55
export type RawAttributes<T> = T & ValidatedAttributes<T>;
@@ -33,13 +33,17 @@ type AttributeUnion = {
3333
};
3434
}[keyof AttributeTypeMap];
3535

36-
export type TypedAttributeValue = AttributeUnion & { unit?: MeasurementUnit };
36+
export type TypedAttributeValue = AttributeUnion & { unit?: AttributeUnit };
3737

3838
export type AttributeObject = {
3939
value: unknown;
40-
unit?: MeasurementUnit;
40+
unit?: AttributeUnit;
4141
};
4242

43+
// Unfortunately, we loose type safety if we did something like Exclude<MeasurementUnit, string>
44+
// so therefore we unionize between the three supported unit categories.
45+
type AttributeUnit = DurationUnit | InformationUnit | FractionUnit;
46+
4347
/* If an attribute has either a 'value' or 'unit' property, we use the ValidAttributeObject type. */
4448
export type ValidatedAttributes<T> = {
4549
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/core/test/lib/scope.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ describe('Scope', () => {
221221
});
222222
});
223223

224-
it('accepts a custom unit', () => {
224+
it('still accepts a custom unit but TS-errors on it', () => {
225225
// mostly there for type checking purposes.
226226
const scope = new Scope();
227+
/** @ts-expect-error we don't support custom units type-wise but we don't actively block them */
227228
scope.setAttribute('str', { value: 3, unit: 'inch' });
228229
expect(scope['_attributes']).toEqual({
229230
str: { value: 3, unit: 'inch' },

0 commit comments

Comments
 (0)