Skip to content

(TypeScript) Property 'msg' has no initializer and is not definitely assigned in the constructor. #834

Closed
@medz

Description

@medz

Version

3.0.0-alpha.12

Steps to reproduce

  1. vue create hello-world 使用 TypeScript 和使用 class-style 风格组件
  2. cd hello-world
  3. yarn serve or yarn build

What is expected?

Success

What is actually happening?

提示:

Property 'msg' has no initializer and is not definitely assigned in the constructor.

没有修改任何文件,创建默认项目后运行报错。

Activity

yyx990803

yyx990803 commented on Feb 13, 2018

@yyx990803
Member

无法重现,可能是你的 npm 缓存问题。看下你的 components/HelloWorld.vue 文件是否是这样的:

export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
}
nothingstopi

nothingstopi commented on Feb 14, 2018

@nothingstopi

I have seen the same error with 3.0.0-alpha.13.
One thing I noticed that main.ts is not generated, but main.js instead.
But main.ts was considered as the entry point. See pic below:
image

So I changed main.js to main.ts, and I saw the error/warnings below.:
image

But the page on the browser is showing correctly.

Darkseal

Darkseal commented on Feb 18, 2018

@Darkseal

The issue is most likely related to the strictPropertyInitialization flag introduced in TypeScript 2.7, which is also set to TRUE by default (see official docs).

If you're using TS 2.7, try to turn it off by adding a "strictPropertyInitialization": false setting to the compilerOptions object within the tsconfig.json root file: if that doesn't fix it, try to temporarily downgrade to TS 2.6 and see if the error persists.

For a detailed explanation of the issue and some possible workarounds/fixes, read this post.

falknerdominik

falknerdominik commented on Aug 20, 2018

@falknerdominik

Hi,

I know that it is a bad habit to comment on closed issues, but what @Darkseal posted is just a workaround. Will the generate support strict property initialization in the future?

adminparry

adminparry commented on Apr 29, 2019

@adminparry

remove "strict": true,

shirshak55

shirshak55 commented on Mar 5, 2020

@shirshak55

@adminparry no why to remove strict? Whats the point of using type system then? As even said using bang to tell typesystem the value is initialized is good choice . Removing strict can allow other bugs thats why i never recommend it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @yyx990803@Darkseal@falknerdominik@medz@adminparry

        Issue actions

          (TypeScript) Property 'msg' has no initializer and is not definitely assigned in the constructor. · Issue #834 · vuejs/vue-cli