Skip to content

Commit

Permalink
[activerecord] Avoiding Promise type definition bugs (error TS2416)
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihirop committed Mar 31, 2021
1 parent f770196 commit 2e8c75b
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,11 @@ describe('ActiveRecord$Base (ActiveRecord$Associations)', () => {

it('should correctly', async () => {
const record = (await TestAssociationHasManyRecord.first<TestAssociationHasManyRecord>()) as TestAssociationHasManyRecord;
const result = (await record.children()) as TestAssociationHasManyChildRecord[];
/**
* I overridden then at runtime because the type definition of then is buggy. Since it affects type inference and bugs, it is avoided by any.
* @bug https://github.com/microsoft/TypeScript/issues/33416
*/
const result = (await record.children()) as any;
expect(result.length).toEqual(2);
expect(result).toEqual([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ describe('ActiveRecord$Base (ActiveRecord$Persistence)', () => {

describe('when return true', () => {
it('should correctly', (done) => {
UpdateRecord.all<UpdateRecord>().then((records: UpdateRecord[]) => {
UpdateRecord.all<UpdateRecord>().rueThen((records: UpdateRecord[]) => {
const record = records[0];
const updateResult = record.update({ name: 'rename' });
expect(updateResult).toEqual(true);
Expand All @@ -626,7 +626,7 @@ describe('ActiveRecord$Base (ActiveRecord$Persistence)', () => {

describe('whenn return false', () => {
it('should correctly', (done) => {
UpdateRecord.all<UpdateRecord>().then((records: UpdateRecord[]) => {
UpdateRecord.all<UpdateRecord>().rueThen((records: UpdateRecord[]) => {
const record = records[1];
const updateResult = record.update({ age: 100 });
expect(updateResult).toEqual(false);
Expand Down Expand Up @@ -670,7 +670,7 @@ describe('ActiveRecord$Base (ActiveRecord$Persistence)', () => {

describe('when return true', () => {
it('should correctly', (done) => {
UpdateOrThrowRecord.all<UpdateOrThrowRecord>().then((records: UpdateOrThrowRecord[]) => {
UpdateOrThrowRecord.all<UpdateOrThrowRecord>().rueThen((records: UpdateOrThrowRecord[]) => {
const record = records[0];
const updateResult = record.updateOrThrow({ name: 'rename' });
expect(updateResult).toEqual(true);
Expand All @@ -682,7 +682,7 @@ describe('ActiveRecord$Base (ActiveRecord$Persistence)', () => {

describe('when throw error', () => {
it('should correctly', (done) => {
UpdateOrThrowRecord.all<UpdateOrThrowRecord>().then((records: UpdateOrThrowRecord[]) => {
UpdateOrThrowRecord.all<UpdateOrThrowRecord>().rueThen((records: UpdateOrThrowRecord[]) => {
const record = records[1];
expect(() => {
record.updateOrThrow({ age: 100 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Record (Scoping)', () => {
describe('[static] all', () => {
describe('when do not exists cahce', () => {
it('should correctly', (done) => {
ScopingRecord.all<ScopingRecord>().then((records: ScopingRecord[]) => {
ScopingRecord.all<ScopingRecord>().rueThen((records: ScopingRecord[]) => {
expect(records).toEqual([
{
__rue_created_at__: '2021-03-05T23:03:21+09:00',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ActiveRecord$Associations$CollectionProxy$Base<
const { holder, scope } = value;

if (scope instanceof Promise) {
scope.then((r) => {
scope.rueThen((r) => {
holder.scope = r as T[];
Evaluator.all(holder);

Expand Down Expand Up @@ -100,7 +100,7 @@ export class ActiveRecord$Associations$CollectionProxy$Base<
): Promise<U> {
return this.superThen(({ holder, scope }) => {
if (scope instanceof Promise) {
return scope.then((records) => {
return scope.rueThen((records) => {
holder.scope = records as T[];
/**
* @description Pass by value so that 「proxy === record」 does not occur
Expand Down
49 changes: 41 additions & 8 deletions packages/activerecord/src/records/relations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ export class ActiveRecord$Relation$Base<
/**
* @see https://gist.github.com/domenic/8ed6048b187ee8f2ec75
* @alias rueThen
*
* @bug typescript bug
* @description Since the type definition of Promise is buggy, it cannot be implemented with the type of then overridden.
* @description If healed, comment out.
* @see https://github.com/microsoft/TypeScript/issues/33416
*
*/
// @ts-expect-error
then(onFulfilled: t.PromiseResolve<T, S>, onRejected?: t.PromiseReject<any>) {
return this.rueThen(onFulfilled, onRejected);
}
// then(onFulfilled: t.PromiseResolve<T, S>, onRejected?: t.PromiseReject<any>) {
// return this.rueThen(onFulfilled, onRejected);
// }

protected superThen(
onFulfilled: t.PromiseResolveHolder<T, H, S>,
Expand All @@ -127,11 +132,15 @@ export class ActiveRecord$Relation$Base<

/**
* @alias rueCatch
*
* @bug typescript bug
* @description Since the type definition of Promise is buggy, it cannot be implemented with the type of then overridden.
* @description If healed, comment out.
* @see https://github.com/microsoft/TypeScript/issues/33416
*/
// @ts-expect-error
catch(errFn: (err: any) => void | PromiseLike<void>) {
return this.rueCatch(errFn);
}
// catch(errFn: (err: any) => void | PromiseLike<void>) {
// return this.rueCatch(errFn);
// }

rueCatch(errFn: (err: any) => void | PromiseLike<void>) {
return (
Expand Down Expand Up @@ -535,3 +544,27 @@ export class ActiveRecord$Relation$Base<
return this.toA();
}
}

/**
* When I define it in a class, I get a Promise type definition error.
* So I'm avoiding type definition errors by overriding then at runtime.
* @bug https://github.com/microsoft/TypeScript/issues/33416
*/
Object.defineProperty(ActiveRecord$Relation$Base.prototype, 'then', {
writable: false,
configurable: false,
enumerable: false,
value: ActiveRecord$Relation$Base.prototype.rueThen,
});

/**
* When I define it in a class, I get a Promise type definition error.
* So I'm avoiding type definition errors by overriding then at runtime.
* @bug https://github.com/microsoft/TypeScript/issues/33416
*/
Object.defineProperty(ActiveRecord$Relation$Base.prototype, 'catch', {
writable: false,
configurable: false,
enumerable: false,
value: ActiveRecord$Relation$Base.prototype.rueCatch,
});
Loading

0 comments on commit 2e8c75b

Please sign in to comment.