Skip to content

Commit cb878ec

Browse files
committed
Fix types
1 parent d36323d commit cb878ec

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

packages/@ember/-internals/glimmer/tests/integration/components/runtime-template-compiler-explicit-test.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,20 @@ moduleFor(
342342
class extends RenderingTestCase {
343343
async '@test Can render a private field value'() {
344344
await this.renderComponentModule(() => {
345-
return class extends GlimmerishComponent {
345+
class TestComponent extends GlimmerishComponent {
346346
// eslint-disable-next-line no-unused-private-class-members
347347
#greeting = 'Hello, world!';
348348

349349
static {
350350
template('<p>{{this.#greeting}}</p>', {
351351
component: this,
352-
scope: (instance: any) => ({
353-
'#greeting': instance?.#greeting,
352+
scope: (instance) => ({
353+
'#greeting': instance ? instance.#greeting : undefined,
354354
}),
355355
});
356356
}
357-
};
357+
}
358+
return TestComponent;
358359
});
359360

360361
this.assertHTML('<p>Hello, world!</p>');
@@ -363,7 +364,7 @@ moduleFor(
363364

364365
async '@test Can render multiple private fields'() {
365366
await this.renderComponentModule(() => {
366-
return class extends GlimmerishComponent {
367+
class TestComponent extends GlimmerishComponent {
367368
// eslint-disable-next-line no-unused-private-class-members
368369
#firstName = 'Jane';
369370
// eslint-disable-next-line no-unused-private-class-members
@@ -372,13 +373,14 @@ moduleFor(
372373
static {
373374
template('<p>{{this.#firstName}} {{this.#lastName}}</p>', {
374375
component: this,
375-
scope: (instance: any) => ({
376-
'#firstName': instance?.#firstName,
377-
'#lastName': instance?.#lastName,
376+
scope: (instance?: InstanceType<typeof TestComponent>) => ({
377+
'#firstName': instance ? instance.#firstName : undefined,
378+
'#lastName': instance ? instance.#lastName : undefined,
378379
}),
379380
});
380381
}
381-
};
382+
}
383+
return TestComponent;
382384
});
383385

384386
this.assertHTML('<p>Jane Doe</p>');
@@ -387,7 +389,7 @@ moduleFor(
387389

388390
async '@test Can use private field method with on modifier'() {
389391
await this.renderComponentModule(() => {
390-
return class extends GlimmerishComponent {
392+
class TestComponent extends GlimmerishComponent {
391393
// eslint-disable-next-line no-unused-private-class-members
392394
#message = 'Hello';
393395

@@ -399,13 +401,14 @@ moduleFor(
399401
static {
400402
template('<button type="button" {{on "click" this.#updateMessage}}>Click</button>', {
401403
component: this,
402-
scope: (instance: any) => ({
404+
scope: (instance?: InstanceType<typeof TestComponent>) => ({
403405
on,
404-
'#updateMessage': instance?.#updateMessage,
406+
'#updateMessage': instance ? instance.#updateMessage : undefined,
405407
}),
406408
});
407409
}
408-
};
410+
}
411+
return TestComponent;
409412
});
410413

411414
this.assertHTML('<button type="button">Click</button>');
@@ -416,20 +419,21 @@ moduleFor(
416419
await this.renderComponentModule(() => {
417420
let Greeting = template('<span>{{yield}}</span>');
418421

419-
return class extends GlimmerishComponent {
422+
class TestComponent extends GlimmerishComponent {
420423
// eslint-disable-next-line no-unused-private-class-members
421424
#name = 'Ember';
422425

423426
static {
424427
template('<Greeting>Hello, {{this.#name}}!</Greeting>', {
425428
component: this,
426-
scope: (instance: any) => ({
429+
scope: (instance?: InstanceType<typeof TestComponent>) => ({
427430
Greeting,
428-
'#name': instance?.#name,
431+
'#name': instance ? instance.#name : undefined,
429432
}),
430433
});
431434
}
432-
};
435+
}
436+
return TestComponent;
433437
});
434438

435439
this.assertHTML('<span>Hello, Ember!</span>');
@@ -444,7 +448,7 @@ moduleFor(
444448
assert.equal(value, 42);
445449
};
446450

447-
return class extends GlimmerishComponent {
451+
class TestComponent extends GlimmerishComponent {
448452
// eslint-disable-next-line no-unused-private-class-members
449453
#secretValue = 42;
450454

@@ -453,16 +457,17 @@ moduleFor(
453457
'<button {{on "click" (fn checkValue this.#secretValue)}}>Click</button>',
454458
{
455459
component: this,
456-
scope: (instance: any) => ({
460+
scope: (instance?: InstanceType<typeof TestComponent>) => ({
457461
on,
458462
fn,
459463
checkValue,
460-
'#secretValue': instance?.#secretValue,
464+
'#secretValue': instance ? instance.#secretValue : undefined,
461465
}),
462466
}
463467
);
464468
}
465-
};
469+
}
470+
return TestComponent;
466471
});
467472

468473
this.click('button');

packages/@ember/template-compiler/lib/template.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export interface ExplicitTemplateOnlyOptions extends BaseTemplateOptions {
8787
* static {
8888
* template('{{this.#greeting}}, {{@place}}!',
8989
* { component: this },
90-
* scope: (instance) => ({ '#greeting': instance?.#greeting }),
90+
* scope: (instance) => ({ '#greeting': instance ? instance.#greeting : undefined }),
9191
* );
9292
* }
9393
* }
@@ -96,7 +96,7 @@ export interface ExplicitTemplateOnlyOptions extends BaseTemplateOptions {
9696
export interface ExplicitClassOptions<
9797
C extends ComponentClass,
9898
> extends BaseClassTemplateOptions<C> {
99-
scope(instance?: InstanceType<C>): Record<string, unknown>;
99+
scope: (instance: InstanceType<C>) => Record<string, unknown>;
100100
}
101101

102102
/**
@@ -262,12 +262,17 @@ function extractPrivateFields(templateString: string): string[] {
262262

263263
export function template(
264264
templateString: string,
265-
options?: ExplicitTemplateOnlyOptions | ImplicitTemplateOnlyOptions
265+
options?: (ExplicitTemplateOnlyOptions | ImplicitTemplateOnlyOptions) & { component?: never }
266266
): TemplateOnlyComponent;
267267

268268
export function template<C extends ComponentClass>(
269269
templateString: string,
270-
options: ExplicitClassOptions<C> | ImplicitClassOptions<C> | BaseClassTemplateOptions<C>
270+
options: ExplicitClassOptions<C>
271+
): C;
272+
273+
export function template<C extends ComponentClass>(
274+
templateString: string,
275+
options: (ImplicitClassOptions<C> | BaseClassTemplateOptions<C> & { scope?: never })
271276
): C;
272277
export function template(
273278
templateString: string,
@@ -295,12 +300,7 @@ export function template(
295300
// component class, which _getProp looks up when it encounters a `#` key.
296301
let originalScopeWithInstance: ((instance: any) => Record<string, unknown>) | undefined;
297302

298-
if (
299-
privateFields.length > 0 &&
300-
options.scope &&
301-
!options.eval &&
302-
options.component
303-
) {
303+
if (privateFields.length > 0 && options.scope && !options.eval && options.component) {
304304
originalScopeWithInstance = options.scope as (instance: any) => Record<string, unknown>;
305305
const origScope = originalScopeWithInstance;
306306

0 commit comments

Comments
 (0)