JavaScript/typescript/error:修订间差异
来自康健生活
< JavaScript | typescript
| (未显示同一用户的3个中间版本) | |||
| 第70行: | 第70行: | ||
* 在写 <syntaxhighlight lang="javascript" inline> | * 在写 <syntaxhighlight lang="javascript" inline> | ||
document.getElementById('root').className = 'blur';</syntaxhighlight> 的时候提示 <code>Object is possibly null</code>,更改 | document.getElementById('root').className = 'blur';</syntaxhighlight> 的时候提示 <code>Object is possibly null</code>,更改 | ||
*#<syntaxhighlight lang="javascript"> | *# <syntaxhighlight lang="javascript" inline> | ||
(document.getElementById('root') as | (document.getElementById('root') as Element).className = 'blur';</syntaxhighlight>: 断言 | ||
*# <syntaxhighlight lang="javascript" inline> | |||
document.getElementById('root')!.className = '';</syntaxhighlight>: 感叹号是非null和非undefined的类型断言,非空断言 | |||
*# <syntaxhighlight lang="javascript" inline> | |||
const rootContainer = document.getElementById('root'); | |||
if(rootContainer) rootContainer.className = 'blur';</syntaxhighlight>: null的判断 | |||
*# <syntaxhighlight lang="javascript" inline> | |||
const rootContainer = document.getElementById('root'); | |||
rootContainer && (rootContainer.className = 'blur');</syntaxhighlight> | |||
* Property 'style' does not exist on type 'Element' <syntaxhighlight lang="javascript" inline> | |||
document.getElementsByClassName('section_content_icon_box_style')[0].style.background = `conic-gradient(#4D4D57 ${45*index}deg, transparent ${45*index}deg ${180 - 45*index}deg, transparent 180deg)`; | |||
</syntaxhighlight> | |||
*# <syntaxhighlight lang="javascript" inline> | |||
(document.getElementsByClassName('section_content_icon_box_style') as HTMLCollectionOf<HTMLElement>)[0].style.background = `conic-gradient(#4D4D57 ${45*index}deg, transparent ${45*index}deg ${180 - 45*index}deg, transparent 180deg)`; | |||
</syntaxhighlight> | |||
* Type 'undefined' is not assignable to type | |||
: 加类型断言 | |||
2021年6月23日 (三) 11:40的最新版本
error 1
- Property 'call' does not exist on type 'object'
- 在项目中,我使用了
await this.axios.call(this.context, {抛出此错误,axios 的设定是 interface HttpInterface { axios: object; context?: Record<string, unknown>; }
- 如果改成
axios: any可消除错误,又或者这样设置 interface AxiosPropertyCall { call: any } interface HttpInterface { axios: AxiosPropertyCall; context?: Record<string, unknown>; } await this.axios.call(this.context
- 在项目中,我使用了
- Property 'username' has no initializer and is not definitely assigned in the constructor.
- 缺少初始化
username: AccountInterface['username'] password: AccountInterface['password'] verify?: AccountInterface['verify'] constructor(props:any) { super(props) this.username = '' this.password = '' this.verify = '' }
- Type 'Login' has no properties in common with type 'HttpParamInterface'.
- 两个类之间没有共同属性
- Cannot find name 'T'
- Type 'string | number' is not assignable to type 'string'.
- 使用 tostring()
- A required parameter cannot follow an optional parameter.
- 必选参数不能位于可选参数后
- Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.
- 原代码 password 缺少默认值
async fetch(username: AccountInterface['username'], password: AccountInterface['password'], httpMethod?: HttpParamInterface['httpMethod']): Promise<object> { const result: FetchData<any> = await this.axios.call(this.context, { method: httpMethod || 'POST', data: { userName: username, password: Base64.encode(password), sign: byteMd5(username, password)
async fetch(username: AccountInterface['username'], password: AccountInterface['password']='', httpMethod?: HttpParamInterface['httpMethod']): Promise<object> { const result: FetchData<any> = await this.axios.call(this.context, { method: httpMethod || 'POST', data: { userName: username, password: Base64.encode(password), sign: byteMd5(username, password)
- 一个需求,在解构时抛错:
const result: FetchData<any> = await this.request.call(null, ...setHttpMethodTypeData({ method: httpMethod || 'get', url: basicsModel.queryMenuData, headers: headers }))
- Type 'object' must have a '[Symbol.iterator]()' method that returns an iterator
error 2
- Calls a defined callback function on each element of an array, and returns an array that contains the results.(map函数相关)
- 在写
document.getElementById('root').className = 'blur';的时候提示Object is possibly null,更改(document.getElementById('root') as Element).className = 'blur';: 断言document.getElementById('root')!.className = '';: 感叹号是非null和非undefined的类型断言,非空断言const rootContainer = document.getElementById('root'); if(rootContainer) rootContainer.className = 'blur';: null的判断const rootContainer = document.getElementById('root'); rootContainer && (rootContainer.className = 'blur');
- Property 'style' does not exist on type 'Element'
document.getElementsByClassName('section_content_icon_box_style')[0].style.background = `conic-gradient(#4D4D57 ${45*index}deg, transparent ${45*index}deg ${180 - 45*index}deg, transparent 180deg)`;(document.getElementsByClassName('section_content_icon_box_style') as HTMLCollectionOf<HTMLElement>)[0].style.background = `conic-gradient(#4D4D57 ${45*index}deg, transparent ${45*index}deg ${180 - 45*index}deg, transparent 180deg)`;
- Type 'undefined' is not assignable to type
- 加类型断言
No categories