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

Enhance type safety in StructuredView #7

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

rubyquaildesign
Copy link

This is super quick and I', sure if this is worth adding unless we also add the ability to type check the values rather then just the keys, but I'm working on putting together a typescript plugin which hopefully will allow typescript to access the type definitions of a script at compile/development time so with this change you could ensure that struct member names match those in the shader

more specific TypeDefinitions lead to stricter and more accurate type
checks.

Signed-off-by: Ruby Quail <ruby@rubyquail.design>
@greggman
Copy link
Owner

greggman commented Jan 13, 2024

Thanks for the PR!

I'm am not sure what this adds. Can you give me an example? I downloaded it locally and tried some stuff but I didn't see the difference (note, I'm not a typescript expert)

If you want something that generates types for typescript, this code might come close with a few tweaks. OTOH, that code might be missing info to fully do it.

@rubyquaildesign
Copy link
Author

const myStructDefinition = {
    size: 12,
    fields: {
        'fFieldOne': {
            offset: 0,
            type:{size: 4 },
        },
        'fFieldTwo': {
            offset: 4,
            type:{size: 4},
        },
        'fFieldThree': {
            offset: 8,
            type:{size:4},
        },
    },
} satisfies StructDefinition;

const myView = makeStructuredView(myStructDefinition);
myView.set({
    fFieldOne: 0.5,
    fFieldThree: .2,
    fFieldTwo: 0.7,
    fFieldFour: 12,
});
// Object literal may only specify known properties, and 'fFieldFour' does not exist in type 'Partial<Record<"fFieldOne" | "fFieldTwo" | "fFieldThree", any>>'.

Effectively just provides type information to the fields if the struct definition has field information in it (which could be provided by a typescript plugin like how typescript-plugin-css-modules does for css modules, giving you autocorrect and in editor error handling for struct field names)

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.

None yet

2 participants