-
Notifications
You must be signed in to change notification settings - Fork 379
Property 'title' has no initializer and is not definitely assigned in the constructor. #81
Comments
I am also facing the same issue. Was giving a try to https://github.com/Microsoft/TypeScript-Vue-Starter |
@yuu2lee4 Hi. What version of TypeScript are you using? http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html |
@kaorun343 Mine was 2.2.1, I update to the latest 2.7.1 but still see the issue. |
Since you use the latest version of TS, you mention the error. So, you'll need to do like this below. @Component
export default class Modal extends Vue {
@Prop()
title!: string;
} |
Are you using VSCode? Then, you need to change a TS Version that the editor use. |
@kaorun343 In my tsconfig I set "strictPropertyInitialization": false. |
ok i noticed the number is 2.7.1 |
@kaorun343 @Hanruis TS2508: No base constructor has the specified number of type arguments. |
It is possible that use the initial value to be default value of props ? example: // source
class MyApp extends Vue{
@Prop()
name = ''
}
// equals
props:{
name:{
type:String,
default:''
}
} |
@Hanruis setting initial value doesn't fixes the issue. |
For me the solution was to use the new "definite assignment assertion modifier" from the documentation http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html @Component
export default class MyComponent extends Vue {
@Prop({ required: true }) title!: string
@Prop() optionalItem: string|undefined
} Notice the exclamation mark in the title parameter. I only use this if required is set to true or if there is a default prop. If not then you should use |undefined. "The definite assignment assertion is a feature that allows a ! to be placed after instance property and variable declarations to relay to TypeScript that a variable is indeed assigned for all intents and purposes, even if TypeScript’s analyses cannot detect so." |
You can simply provide an initial value for those properties: @Prop() name: string = "";
@Prop() initialEnthusiasm: number = 0; |
I was able to fix this issue by adding a constructor to my component: import { Vue, Component, Prop } from 'vue-property-decorator';
import Radio from './radio';
@Component
export default class RadioComponent extends Vue {
@Prop() radio: Radio;
constructor() {
super();
}
} |
Hello guys, I have found this thread in stack overflow. It seems to be an issue with typescript version 2.7.*. A few configuration settings in .tsconfig should do the trick - the accepted answer should provide you more info. I hope this helps! :) P.S: It comes from an angular thread so do not hate me about it 😆 |
correct, see vuejs/vue-cli#834 (comment) |
I needed to turn this off in tsconfig … broke all my @angular ts files |
@Gesh4o that worked for me. I forget that ALL VS Code instances have to be closed for it to take effect... 2.7.2 works for me, using the latest FEB 2018 VS Code update. |
still reproduce in ts 2.8.1 |
Props need to have an exclamation mark appended, i.e. From https://github.com/Microsoft/TypeScript-Vue-Starter#using-decorators-to-define-a-component:
|
If I don't use constructor, then I have to define state in the store as optional or with an initial value: Setting a default value or forcing a definite value with an exclamation mark(!) defeats the purpose of Typescript and adds more code. Now, every method that returns a state or data or prop has to return the interface of that variable | undefined. Without a constructor, class basically becomes an interface. |
"State values should never be initialized undefined on purpose. It makes them loose reactivity. Initialize them as null if you want to." |
You may mark the field as optional upon initialization, like so: import React from 'react';
class MyComponent extends React.Component {
// * note the '?'
myField?: any;
} |
@component
export default class Modal extends Vue {
@prop()
title: string;
}
It report this error when using prop decorator
The text was updated successfully, but these errors were encountered: