Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make and and or return type NonNullable #191

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions packages/ember-truth-helpers/src/helpers/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ interface AndSignature<T extends unknown[]> {
Args: {
Positional: T;
};
Return: T[number];
Return: NonNullable<T[number]>;
}

// We use class-based helper to ensure arguments are lazy-evaluated
// and helper short-circuits like native JavaScript `&&` (logical AND).
export default class AndHelper<T extends unknown[]> extends Helper<
AndSignature<T>
> {
public compute(params: T): T[number] {
public compute(params: T): NonNullable<T[number]> {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === false) {
return params[i];
return params[i] as NonNullable<T[number]>;
}
}
return params[params.length - 1];
return params[params.length - 1] as NonNullable<T[number]>;
}
}
8 changes: 4 additions & 4 deletions packages/ember-truth-helpers/src/helpers/or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ interface OrSignature<T extends unknown[]> {
Args: {
Positional: T;
};
Return: T[number];
Return: NonNullable<T[number]>;
SergeAstapov marked this conversation as resolved.
Show resolved Hide resolved
}

// We use class-based helper to ensure arguments are lazy-evaluated
// and helper short-circuits like native JavaScript `||` (logical OR).
export default class OrHelper<T extends unknown[]> extends Helper<
OrSignature<T>
> {
public compute(params: T): T[number] {
public compute(params: T): NonNullable<T[number]> {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === true) {
return params[i];
return params[i] as NonNullable<T[number]>;
}
}
return params[params.length - 1];
return params[params.length - 1] as NonNullable<T[number]>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import templateOnly from '@ember/component/template-only';
interface Signature {
Element: HTMLDivElement;
Args: {
andArg: object | boolean;
orArg: object | boolean;
andArg: object | string[];
orArg: object | string[];
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
{{xor true false}}

<AndOrTypeChecking
@andArg={{or (hash) (array)}}
@orArg={{and (hash) (array)}}
@andArg={{or this.objectOrUndefined this.arrayOrUndefined}}
@orArg={{and this.objectOrUndefined this.arrayOrUndefined}}
Comment on lines -15 to +16
Copy link
Contributor

@Techn1x Techn1x Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andArg={{or ...}}
@orArg={{and ...}}

I don't quite understand the test but, are these the wrong way around?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this test case needs updates to better reflect use real world scenarios.
Will do the same prob after #196

/>
11 changes: 11 additions & 0 deletions packages/modern-test-app/app/components/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@glimmer/component';

export default class extends Component {
get objectOrUndefined(): object | undefined {
return new Date().getMonth() === 7 ? { foo: 'bar' } : undefined;
}

get arrayOrUndefined(): object | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't an array 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeap! it is. I had a huge doubts about this approach in the first place and more I think about it, the more I get confident this is totally wrong.
I think #196 if the way to go (just need to make CI happy)

return new Date().getMonth() === 6 ? ['foo', 'bar'] : undefined;
}
}