TypeScript Errors All In One

1. Property 'name' has no initializer and is not definitely assigned in the constructor.ts

属性"名称"没有初始化程序,并且在构造函数中未明确分配

Strict Class Initialization

--strictPropertyInitialization

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#strict-class-initialization

class C {
foo: number;
bar = "hello";
baz: boolean;
// Property 'baz' has no initializer and is not definitely assigned in the constructor.ts(2564)
constructor() {
this.foo = 42;
}
}

solutions

class CC {
foo: number;
bar = "hello";
baz: boolean | undefined;
constructor() {
this.foo = 42;
}
}

  1. tsconfig.json 关闭 Strict Class Initialization

tsconfig.json

{
"include": ["src/**/*"],
"exclude": ["tests/**/*"],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "preserve",
"declaration": false,
"declarationMap": false,
"sourceMap": true,
"outDir": "./build",
"rootDir": "./",
"removeComments": true,
"downlevelIteration": true,
"isolatedModules": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,// 关闭
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowUmdGlobalAccess": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}

2. declaration for the 'Promise' constructor or include 'ES2015' in your --lib option

interface Promise Represents the completion of an asynchronous operation

An async function or method in ES5/ES3 requires the 'Promise' constructor.

Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option.ts(2705)


const getRequestTo = async <R>(endpoint: string): Promise<ApiResponseInterface<R>> => {
const request = await fetch(process.env.HOST + endpoint);
const response = await request.json();
return response;
}; const userResponse = getRequestTo('/user-data');

solution, How to fixed TypeScript Promise Error

just need to add a line of code"lib": ["es2015"], in your tsconfig.json

{
// ...
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015"],//
// ...
}
}

Cannot find name 'fetch'.ts(2304)

solution, How to fixed TypeScript fetch Error

just need to add a line of code"lib": ["DOM"], in your tsconfig.json

{
// ...
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015", "DOM"],//
// ...
}
}

refs

https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc

https://stackoverflow.com/questions/54104187/typescript-complain-has-no-initializer-and-is-not-definitely-assigned-in-the-co/54104796



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 让BASH,VIM美美的Powerline
  2. 分布式架构高可用架构篇_07_MySQL主从复制的配置(CentOS-6.7+MySQL-5.6)
  3. ArcGIS Server发布服务,报错001270
  4. 个人博客 week1
  5. DNS resolving 占用大量日志
  6. Spring boot 内存优化
  7. 为什么喜欢Kindle
  8. Codeforces Round #205 (Div. 2) : B
  9. Day01_UNIX基础及VI简介
  10. ORACLE 如何查询被锁定表及如何解锁释放session
  11. .net图片自动裁剪白边函数案例
  12. 自动清理SQLServerErrorLog错误日志避免太大
  13. JStorm与Storm源码分析(五)--SpoutOutputCollector与代理模式
  14. 将 Intent 序列化,像 Uri 一样传递 Intent!!!
  15. 快速开发 jQuery 插件的 10 大技巧
  16. python函数的面向对象——面向对象设计
  17. zabbix监控mysql性能
  18. canvas绘图history妙用
  19. OpenJ_Bailian 2814 拨钟问题
  20. Spark SQL 之 Join 实现

热门文章

  1. How Interfaces Work in Go
  2. based on Greenlets (via Eventlet and Gevent) fork 孙子worker 比较 gevent不是异步 协程原理 占位符 placeholder (Future, Promise, Deferred) 循环引擎 greenlet 没有显式调度的微线程,换言之 协程
  3. js控制页面元素值
  4. 从一片森林(JavaScript)到另一片森林(C++)
  5. MySql(五)SQL优化-优化SQL语句的一般步骤
  6. 网络编程(socket简介)
  7. linux系统资源限制———ulimit命令
  8. linux 系统磁盘管理(主分区和逻辑分区)
  9. (19)ln命令:在文件之间建立链接(硬链接和软链接)
  10. MyBatis框架使用 —— 传递多个参数的方式