首页 > 解决方案 > Property 'searchText' has no initializer and is not definitely assigned in the constructor?

问题描述

There is following variable in class:

public searchText: string;

When strict mode is enabled in TypeScript, I see the following error:

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

In fact when I put '' like:

public searchText: string = "";

It solves my problem, but why if default definition string is alreadyempty string;

标签: typescripttypescript2.0

解决方案


if you dont assign value to a variable in javascript then variables have 'undefined' value in it.

so here if you do like this

public searchText: string;

then searchText=undefined and not an empty string. You can do console log you will get undefined


推荐阅读