tsconfig.json
{
"compilerOptions": {
"outDir": "build/dist",
"module": "esnext",
"target": "esnext",
"lib": ["esnext", "dom"],
"sourceMap": true,
"baseUrl": ".",
"jsx": "react-jsx",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"allowJs": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"strict": true,
"paths": {
"@/*": ["./src/*"],
"@@/*": ["./src/.umi/*"],
}
}
}
eslintrc.js
module.exports = {
extends: [require.resolve('@umijs/fabric/dist/eslint')],
globals: {
ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: true,
page: true,
REACT_APP_ENV: true,
},
};
更新问题
class A {
readonly sdkType ='a'
constructor () {
}
}
class B {
readonly sdkType ='b'
constructor () {
}
}
function test(this: A): 1;
function test(this: B): 2;
function test(this:A|B) {
if (this instanceof A) {
return 1
} else {
return 2
}
}
// 这里在调用的时候又报错
const d = test.call(new A)
我的sdkType 就是做唯一类型判断的
原问题
function test<T extends string | number>(params: T): T extends number ? 1 : 2 {
// 这里做了类型判断
if (typeof params === 'number') {
// 这里报错了
return 1
} else {
// 这里报错了
return 2
}
}
P.S. 这可读性挺差的,你这需求不如用函数重载了。