Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/@ember/-internals/metal/lib/tracked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ interface TrackedDecoratorOptions {
description?: string;
}

// `PropertyDecorator` covers the `(target, key)` call TypeScript emits for
// native class fields; `ExtendedMethodDecorator` covers the classic-class call.
export type TrackedDecorator = ExtendedMethodDecorator & PropertyDecorator;

/**
* `tracked` as a decorator factory: `@tracked({ equals })`, or on classic
* classes `tracked({ value })` / `tracked({ initializer })`.
*/
export function tracked(propertyDesc: TrackedDecoratorOptions): ExtendedMethodDecorator;
export function tracked(propertyDesc: TrackedDecoratorOptions): TrackedDecorator;
/**
* `tracked` as a bare decorator: `@tracked foo = 1`.
*/
Expand All @@ -145,7 +149,7 @@ export function tracked<Value>(
): TrackedValue<Value>;
export function tracked(
...args: any[]
): ExtendedMethodDecorator | DecoratorPropertyDescriptor | TrackedValue<any> {
): TrackedDecorator | DecoratorPropertyDescriptor | TrackedValue<any> {
assert(
`@tracked can only be used directly as a native decorator. If you're using tracked in classic classes, add parenthesis to call it like a function: tracked()`,
!(isElementDescriptor(args.slice(0, 3)) && args.length === 5 && args[4] === true)
Expand Down Expand Up @@ -225,7 +229,7 @@ function isDecoratorOptions(value: unknown): value is TrackedDecoratorOptions {
return Object.keys(value).every((key) => DECORATOR_OPTION_KEYS.includes(key));
}

function makeTrackedDecorator(propertyDesc?: TrackedDecoratorOptions): ExtendedMethodDecorator {
function makeTrackedDecorator(propertyDesc?: TrackedDecoratorOptions): TrackedDecorator {
if (DEBUG && propertyDesc) {
assert(
`The options object passed to tracked() may only contain a 'value' or an 'initializer' property, not both. Received: [${Object.keys(
Expand Down Expand Up @@ -275,7 +279,7 @@ function makeTrackedDecorator(propertyDesc?: TrackedDecoratorOptions): ExtendedM

setClassicDecorator(decorator);

return decorator;
return decorator as TrackedDecorator;
}

if (DEBUG) {
Expand Down
3 changes: 3 additions & 0 deletions type-tests/@glimmer/tracking-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ tracked(0, { equals: (a: string, b: string) => a === b });
// ------- decorator forms -------
class Counter {
@tracked count = 0;

@tracked({ equals: Object.is }) another = 0;
}

expectTypeOf(new Counter().count).toEqualTypeOf<number>();
expectTypeOf(new Counter().another).toEqualTypeOf<number>();

// classic class form returns a decorator
expectTypeOf(tracked({ value: 'Zoey' })).toMatchTypeOf<Function>();
Expand Down
Loading