Skip to content

Commit

Permalink
fix(type): make sure typeof x expression doesn't return the origina…
Browse files Browse the repository at this point in the history
…l type
  • Loading branch information
marcj committed Nov 16, 2023
1 parent 92315f9 commit 7206e7e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/framework/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export class FrameworkModule extends createModule({
//only register the RPC controller
this.addImport(new ApiConsoleModule({ listen: false, markdown: '' }).rename('internalApi'));

this.addProvider(DebugBroker);
this.addListener(onAppShutdown.listen(async (
event, broker: DebugBroker, store: StopwatchStore) => {
await store.close();
Expand All @@ -209,6 +208,7 @@ export class FrameworkModule extends createModule({
// this.setupProvider(LiveDatabase).enableChangeFeed(DebugRequest);
}

this.addProvider(DebugBroker);
this.addProvider(FileStopwatchStore);
this.addProvider({ provide: StopwatchStore, useExisting: FileStopwatchStore });
this.addProvider({
Expand Down
4 changes: 2 additions & 2 deletions packages/sqlite/tests/integration-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test('check type structure', () => {
const group = ReflectionClass.from(Group);
const userGroup = ReflectionClass.from(UserGroup);

expect(userGroup.type.parent).toBeUndefined();

expect(userGroup.getProperty('id').isPrimaryKey()).toBe(true);
expect(userGroup.getProperty('user').isReference()).toBe(true);

Expand All @@ -17,8 +19,6 @@ test('check type structure', () => {
expect(userGroup.getProperty('group').isReference()).toBe(true);
expect(userGroup.getProperty('group').getResolvedReflectionClass().getPrimary()).toBeInstanceOf(ReflectionProperty);

expect(userGroup.type.parent).toBeUndefined();

const groupsType = user.getProperty('groups').type;
const userGroupsElementType = user.getProperty('groups').getSubType();
//due to a change in copyAndSetParent being shallow copy only this is no longer true
Expand Down
4 changes: 2 additions & 2 deletions packages/type/src/reflection/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ export function typeInfer(value: any): Type {
//with emitted types: function or class
//don't use resolveRuntimeType since we don't allow cache here
// console.log('typeInfer of', value.name);
return Processor.get().reflect(value) as Type;
return Processor.get().reflect(value, undefined, {inline: true}) as Type;
}

if (isClass(value)) {
Expand All @@ -1761,7 +1761,7 @@ export function typeInfer(value: any): Type {
if ('function' === typeof constructor && constructor !== Object && isArray(constructor.__type)) {
//with emitted types
//don't use resolveRuntimeType since we don't allow cache here
return Processor.get().reflect(constructor) as Type;
return Processor.get().reflect(constructor, undefined, {inline: true}) as Type;
}

if (constructor === RegExp) return { kind: ReflectionKind.regexp };
Expand Down
2 changes: 1 addition & 1 deletion packages/type/tests/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ test('correct T resolver', () => {
console.log('js', js);
const type = transpileAndReturn(code) as (v: any) => ClassType;
const classType = type('abc');
expectEqualType(typeInfer(classType), {
expectEqualType(reflect(classType), {
kind: ReflectionKind.class,
classType: classType,
types: [
Expand Down
15 changes: 15 additions & 0 deletions packages/type/tests/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1652,3 +1652,18 @@ test('issue-495: extend Promise in union', () => {
expect(type.literal).toBe(false);
}
});

test('used type does not leak parent to original', () => {
class User {
groups!: {via: typeof UserGroup};
}

class UserGroup {
public user!: User;
}

const user = typeOf<User>();
const userGroup = typeOf<UserGroup>();

expect(userGroup.parent).toBe(undefined);
});

0 comments on commit 7206e7e

Please sign in to comment.