TypeScript常見報錯

TS1192: Module '"/Volumes/repos/tc-web-ts/node_modules/@types/react/index"' has no default export.javascript

把"allowSyntheticDefaultImports": true添加到tsconfig.jsonjava

TS7016: Could not find a declaration file for module 'rc-queue-anim'. '/Users/michealye/Desktop/myProduct/shebao520-mobile/node_modules/rc-queue-anim/lib/index.js' implicitly has an 'any' type.node

Try npm install @types/rc-queue-anim if it exists or add a new declaration (.d.ts) file containing declare module 'rc-queue-anim';react

Another two ways, when a module is not yours - just try install its from @types:web

npm install -D @types/module-name Or, if install errored - try rewrite import to require:typescript

// import * as yourModuleName from 'module-name'; const yourModuleName = require('module-name');express

TS7016: Could not find a declaration file for module '../styledComponents/loginAndRegister'. '/Users/michealye/Desktop/myProduct/shebao520-mobile/src/components/styledComponents/loginAndRegister.jsx' implicitly has an 'any' type.npm

沒有指定類型 Edit your tsconfig.json "noImplicitAny": falsejson

TS1238: Unable to resolve signature of class decorator when called as an expression.bash

不能這樣

@connect(mapStateToProps,mapDispatchToProps)

只能這樣 export default connect( mapStateToProps, mapDispatchToProps )(LogTable)

TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.

In VSCode, Go to File => Preferences => Settings (or Control+comma) and it will open the User Settings file. Add "javascript.implicitProjectConfig.experimentalDecorators": true to the file and it should fix it.

in tsconfig.json

{ "compilerOptions": { "experimentalDecorators": true, "allowJs": true } }

TS1042: 'async' modifier cannot be used here.

Your usage is totally wrong. eg: async () => { /* ... */ }

TS2705: 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.

{ "compilerOptions": { "lib": [ "es2015" ] } }

TS2304: Cannot find name 'CSSStyleDeclaration'.

Adding "dom" to my compilerOptions.lib array in tsconfig.json did the trick.

TS1127 The issue is caused by a copy/past and just added a hidden invalid character. I rewrote the code manually from scratch everything work well now.

TS1005,TS1161,TS2352多數是由於文件後綴的致使的,快把ts改成tsx吧 ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts [tsl] ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts(5,13) TS1005: '>' expected.

ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts [tsl] ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts(5,14) TS1161: Unterminated regular expression literal.

ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts [tsl] ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts(6,1) TS1005: ')' expected.

ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts [tsl] ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts(5,8) TS2352: Type 'RegExp' cannot be converted to type 'App'.

ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts [tsl] ERROR in /Users/michealye/Desktop/myProduct/nodeStudy/styled-components-server-side-rendering/src/client/index.ts(5,8) TS2352: Type 'RegExp' cannot be converted to type 'App'. Property 'render' is missing in type 'RegExp'.

TS2345/TS2322: Argument of type '{ username: Key; }' is not assignable to parameter of type 'LoginXXX' TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. 多數是由於有一個地方沒有區分類型吧 it is called not setting --strictNullCheck 😉. I would say it is desirable behaviour that if you have said that you want to make sure you don't have null values, that the type system becomes more strict. You can of course be explicit about it (x: any[] = []) and it works.

TS2322: Type '{ children: any; key: string; func: any; className: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'. Property 'func' does not exist on type 'DetailedHTMLProps<HTMLAttributes, HTMLDivElement>'.

typescript不容許隨便寫HTML的屬性,若是要自定義屬性,須要加上data-*

TS2339: Property 'dataset' does not exist on type 'EventTarget'.

加個any

<div onClick={(e: any) => {
                    keyboardMessage[ e.target.dataset.func ]();
                }}>
複製代碼

**TS2339: Property 'getBoundingClientRect' does not exist on type 'Element | Text'. Property 'getBoundingClientRect' does not exist on type 'Text'.

//原來是這樣的
 ReactDOM.findDOMNode(this.thisRef).getBoundingClientRect();
 
//解決辦法是這樣的
    interface Node {
        getBoundingClientRect: () => any
    }
    const node:Node = ReactDOM.findDOMNode(this.thisRef) as Element;
    this.thisClientRect = node.getBoundingClientRect();
複製代碼
相關文章
相關標籤/搜索