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

Update samples to work with TypeScript strict mode (part 2) #285

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

nash1111
Copy link
Contributor

@nash1111 nash1111 commented Jul 29, 2023

@kainino0x
I set strict:true and made sure the npm run build went through
This PR makes all the remaining examples work in TypeScript strict mode. Could you please take a look when you have a moment? Thank you in advance.
Ref: #278

@nash1111 nash1111 changed the title chore: wip adding type chore: modify compilerOption strict true Jul 30, 2023
@nash1111 nash1111 marked this pull request as ready for review July 30, 2023 11:36
@nash1111 nash1111 changed the title chore: modify compilerOption strict true Update samples to work with TypeScript strict mode (part 2) Jul 30, 2023
@kainino0x kainino0x self-requested a review August 1, 2023 20:03
Copy link
Collaborator

@kainino0x kainino0x left a comment

Choose a reason for hiding this comment

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

Overall this is adding too much lines-of-code overhead to make TypeScript happy, which isn't good for readability. Some overhead is needed, but I think we can reduce it.

I've commented on a few things that should significantly improve the readability. Most of my suggestions apply to many places later in the PR - please apply them in all similar situations. (Or update one or two and ping me, if you'd like an incremental review of particular changes before you go through and do all of them.)

@@ -198,7 +199,7 @@ const init: SampleInit = async ({ canvas, pageState }) => {
const renderPassDescriptor: GPURenderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
view: undefined as any, // Assigned later
Copy link
Collaborator

@kainino0x kainino0x Aug 2, 2023

Choose a reason for hiding this comment

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

(In response to the GitHub Actions check warning)
It would be nice to make the build warning-free. In this case it can probably be view: undefined!,

Copy link
Contributor Author

@nash1111 nash1111 Aug 15, 2023

Choose a reason for hiding this comment

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

@kainino0x
I wanted to use view: undefined! too, but it was inferred as the never type and didn't work out. How about using view: undefined as unknown as GPUTextureView ? Using as twice isn't intuitive, but since we can't directly cast undefined to GPUTextureView, it seems unavoidable. If there's a better suggestion, I'd love to hear it.

src/sample/cornell/main.ts Outdated Show resolved Hide resolved
src/sample/cornell/main.ts Outdated Show resolved Hide resolved
gui.add(params, 'renderer', ['rasterizer', 'raytracer']);
gui.add(params, 'rotateCamera', true);
gui?.add(params, 'renderer', ['rasterizer', 'raytracer']);
gui?.add(params, 'rotateCamera', true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this can't actually happen I think I'd prefer gui! over gui?
If it's wrong it'll just throw a TypeError which is fine for a sample.

Or just assert(gui).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review. I used assert because non-null assertions are discouraged in some lint settings

src/sample/cornell/main.ts Outdated Show resolved Hide resolved
@@ -245,6 +246,7 @@ const init: SampleInit = async ({ canvas, pageState }) => {
function frame() {
// Sample is no longer the active page.
if (!pageState.active) return;
assert(device, 'device is null');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly shouldn't be necessary, though I'm less certain about this since it's inside a function. OK to keep if needed.


assert(attachment, 'attachment is null');

attachment.view = context.getCurrentTexture().createView();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a complicated way to get colorAttachments[0]. How about storing colorAttachment0 into its own variable?

const colorAttachment0: GPURenderPassColorAttachment = {
  view: undefined as any, // Assigned later
};

const renderPassDescriptor: GPURenderPassDescriptor = {
  colorAttachments: [colorAttachment0],
colorAttachment0.view = context.getCurrentTexture().createView();

@nash1111
Copy link
Contributor Author

nash1111 commented Aug 2, 2023

Thank you for the review & I apologize for the late response. I will incorporate your feedback.

@kainino0x
Copy link
Collaborator

Thanks, no rush, thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants