Vue-cli: (TypeScript) Property 'msg' has no initializer and is not definitely assigned in the constructor.

Created on 13 Feb 2018  ·  6Comments  ·  Source: vuejs/vue-cli

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.

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

Most helpful comment

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.

All 6 comments

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

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

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.

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.

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?

remove "strict": true,

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joshuajohnson814 picture joshuajohnson814  ·  3Comments

JIANGYUJING1995 picture JIANGYUJING1995  ·  3Comments

brandon93s picture brandon93s  ·  3Comments

miyamoto-san picture miyamoto-san  ·  3Comments

chasegiunta picture chasegiunta  ·  3Comments