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

Uncaught uninitialized class member with Symbol key #40617

Closed
soul-codes opened this issue Sep 17, 2020 · 4 comments · Fixed by #45974
Closed

Uncaught uninitialized class member with Symbol key #40617

soul-codes opened this issue Sep 17, 2020 · 4 comments · Fixed by #45974
Labels
Bug A bug in TypeScript
Milestone

Comments

@soul-codes
Copy link

TypeScript Version: 4.1.0-dev.20200917

Search Terms:
symbol class member uninitialized

Code

const Foo = Symbol()

class WithFoo {
  [Foo]: any[]; // this shouldn't be allowed without an initialization in a constructor
  Foo: any[]; // compare with this, which errors as expected
}

console.log(new WithFoo()[Foo].length) // uncaught uninitialized symbol member, oops

Expected behavior:
An error like Property 'Foo' has no initializer and is not definitely assigned in the constructor should also be emitted for the symbolic property Foo.

Actual behavior:
No such error is thrown.

Playground Link:
here

@andrewbranch
Copy link
Member

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 18, 2020
@a-tarasyuk
Copy link
Contributor

a-tarasyuk commented Nov 9, 2020

isPropertyInitializedInConstructor doesn't support ElementAccessExpression, only PropertyAccessExpression.

function isPropertyInitializedInConstructor(propName: Identifier | PrivateIdentifier, propType: Type, constructor: ConstructorDeclaration) {
    const reference = factory.createPropertyAccessExpression(factory.createThis(), propName);
    setParent(reference.expression, reference);
    setParent(reference, constructor);
    reference.flowNode = constructor.returnFlowNode;
    const flowType = getFlowTypeOfReference(reference, propType, getOptionalType(propType));
    return !(getFalsyFlags(flowType) & TypeFlags.Undefined);
}

I think isPropertyInitializedInConstructor can be extended to use both ElementAccessExpression and PropertyAccessExpression, however, I'm not sure that getFlowTypeOfReference can resolve the right type for ElementAccessExpressions with Identifiers.

const Foo = Symbol()
class WithFoo {
  [Foo]: any[];
  ["prop"]: any[];

  constructor() {
    this[Foo] = []; ??
    this["prop"] = []; // Works
  }
}

@andrewbranch What do you think?

@andrewbranch
Copy link
Member

@a-tarasyuk you’re right, that would require #36230. Maybe the current behavior is intentional, then, as we’re looking at a choice between always¹ issuing the uninitialized error or never issuing it. 😕

¹ “always,” unless the initialization is on the class field itself, or the error is suppressed with !. These are pretty good as workarounds go, but I’m not sure they’re good enough to merit this change.

@andrewbranch
Copy link
Member

I don’t know though, it seems like issuing the error when there is no constructor would be better than nothing...

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

Successfully merging a pull request may close this issue.

3 participants