Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better tsconfig.json #16542

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["es2015", "es2017", "dom"],
"lib": ["es2015", "es2017", "es2020", "dom"],
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "./bin/.ts",
"rootDir": "./",
"noEmit": true,

"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.typescriptlang.org/tsconfig#strictNullChecks

Why set strictNullChecks to false ? I think strict null check is needed.

"strictPropertyInitialization": false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.typescriptlang.org/tsconfig#strictPropertyInitialization
Why set strictPropertyInitialization to false ?

"strictFunctionTypes": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is true, but, yes, it's better to set it to true explicitly here.

Copy link
Contributor Author

@finscn finscn Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is true, but, yes, it's better to set it to true explicitly here.



// 如果 "strictPropertyInitialization" 为 true , 会面临下面的问题:


class KlassFoo {

}


class KlassA {

    // 必须实例化一个 KlassFoo, 显然是不科学的.  foo 有可能需要延迟创建.
    foo: KlassFoo = new KlassFoo();

}

class KlassB {

    // 需要被迫增加一个 undefined , 破坏了 foo 的原本的意义,
    // 且代码变得冗繁, 到最后项目里会有非常多的 `| undefined`
    foo: KlassFoo | undefined;

}

class KlassC {

    // 被迫加一个 ! , 来欺骗编译器, 这么做是非常危险的
    foo: KlassFoo = null!

}

/*

按照 strictPropertyInitialization 的设计, 其实只有 KlassA 是正道.
后面的两种方案都是为了屏蔽编译器报错 而采取的 "欺骗手段"
但是在实际项目里, KlassA 的做法显然也是不可取的.

成员变量(属性)的类型声明, 本身就是为了声明类型,  而不是创建这个成员变量.
ts里 strictPropertyInitialization=true 的设计, 强迫把声明过程 和 实例化过程混在一起,
是一个 并不好的设计.

*/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strictNullChecks 为什么要设置成 false 是一个更复杂的话题.
有空再说吧

"strictBindCallApply": true,
"noImplicitThis": true,
"alwaysStrict": true,

"noEmit": true,
"downlevelIteration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"isolatedModules": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,

"baseUrl": "./",
"paths": {
"cc.decorator": [
Expand Down Expand Up @@ -51,6 +62,7 @@
]
},
"include": [
"cocos/**/*.ts",
"exports/**/*.ts",
"typedoc-index.ts",
"pal/**/*.ts"
Expand Down
Loading