-
Notifications
You must be signed in to change notification settings - Fork 179
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
Property 'blockId' has no initializer and is not definitely assigned in the constructor. #1273
Comments
Some extra info. I have this in the tsconfig: "experimentalDecorators": true,
"useDefineForClassFields": false, And it's the same whether or not I add the options in the |
The error "Property '...' has no initializer and is not definitely assigned in the constructor." is raised if you haven't initialized a property. For example the following code: class ExampleClass {
name: string
// ~~~~ Property 'name' has no initializer and is not definitely assigned in the constructor.
} Repro in TypeScript playground This can be fixed in a couple ways. The simplest way is to initialize @customElement('rune-block-actions')
export class RuneBlockActions extends LitElement {
@property()
blockId = ""; If for some reason you have a reason to not initialize the property, you can opt-out of setting an initial value with class ExampleClass {
// Don't do this unless you know what you are doing.
// TypeScript now types `name` to be a string, but it'll be `undefined` if no one initializes it.
name!: string
} |
The problem here is that the examples in the lit docs don't do any of what you described above. You need to set class MyElement extends LitElement {
@property()
name: string;
} class MyElement extends LitElement {
@property({type: String})
mode: string; I would say that the takeaway here is that there needs to be a prominent section on "experimentalDecorators": true,
"useDefineForClassFields": false,
"strictPropertyInitialization": false |
You raise a great point about our documentation. We definitely need to address that. Thank you for calling it out. The examples you've pulled from the documentation should be changed to: class MyElement extends LitElement {
@property()
name?: string;
} and class MyElement extends LitElement {
@property({type: String})
mode?: string; I wouldn't recommend globally changing What do you think about those proposed changes? |
I think that makes sense. Thanks very much for the clarification! |
Which package(s) are affected?
Lit Core (lit / lit-html / lit-element / reactive-element)
Description
I'm defining a property like this:
I get this TS error in the editor:
Reproduction
The code is unfortunately closed-source, and I cannot reproduce on the playground.
Workaround
I have not found a workaround.
Is this a regression?
No or unsure. This never worked, or I haven't tried before.
Affected versions
3.0.2
Browser/OS/Node environment
Browser: Chrome 119.0.6045.105
OS: Mac 14
Node: 18.6.1
npm: 9.5.1
The text was updated successfully, but these errors were encountered: