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

Validate import namespace specified functions properly #143

Closed
kungfooman opened this issue Feb 18, 2024 · 0 comments · Fixed by #144
Closed

Validate import namespace specified functions properly #143

kungfooman opened this issue Feb 18, 2024 · 0 comments · Fixed by #144
Assignees
Labels
Runtime type assertions Everything related to type assertions. typings Everything related to type declarations

Comments

@kungfooman
Copy link
Owner

image

This error is caused by:

if (typeof expect === 'string' && expect.includes('.')) {
// E.g. ['MathUtils', 'Quaternion']
const parts = expect.split('.');
if (parts.length === 2) {
const [a, b] = parts;
if (importNamespaceSpecifiers[a]) {
const expectWhat = typeof importNamespaceSpecifiers[a][b];
// console.log("expectWhat", expectWhat);
expect = expectWhat;
}
} else {
console.log("import namespace specifier rewrite unhandled", {expect, parts});
}
}

Clearly the instance of a class isn't a function - otherwise the logic works for other primitive types like numbers.

So I need some special casing for the case of importNamespaceSpecifiers[a][b].constructor

We may be tempted to use importNamespaceSpecifiers[a][b].name, but I don't think this goes far enough - after all you can make nameless classes/functions:

function a() {
    return () => {}
}
f = a()

image

Or in more familiar terms:

function makeVec3Class() {
    return function() {
        this.x = 1;
        this.y = 2;
        this.z = 3;
    }
}
const Vec3 = makeVec3Class();
vec = new Vec3

Output:

image

It seems that TypeScript is itself confused:

/**
 * @param {B.Vec3} vec - The vector.
 */
function testVec(vec) {
  console.log("testVec got", {vec});
}
testVec(new B.Vec3());

image

Error:

Argument of type '(Anonymous function)' is not assignable to parameter of type 'typeof (Anonymous function)'.
  Type '(Anonymous function)' provides no match for the signature '(): void'.ts(2345)
(alias) new Vec3(): (Anonymous function)
export Vec3

Right now I'm tempted to imply that any import namespace specifier function assumes an object with given function as prototype:

if (value.constructor === importNamespaceSpecifiers[a][b]) {
  return true;
}
@kungfooman kungfooman added type assertions Everything related to type assertions. typings Everything related to type declarations Runtime labels Feb 18, 2024
@kungfooman kungfooman self-assigned this Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Runtime type assertions Everything related to type assertions. typings Everything related to type declarations
Projects
None yet
1 participant