第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

對(duì)象可能在 Typescript 中的字符串值“未定義”

對(duì)象可能在 Typescript 中的字符串值“未定義”

我是 Typescript 的新手,目前正在將我們的應(yīng)用程序從 React JSX 轉(zhuǎn)換為 TS,所以如果我誤解了錯(cuò)誤,請(qǐng)告訴我。我得到Object is possibly 'undefined'一個(gè)prop string是從父母?jìng)飨聛?lái)的component。所述string被內(nèi)限定INITIAL_STATE在`作為父private static INITIAL_STATE = {  password: ''};這意味著子組件中的propforpassword永遠(yuǎn)不應(yīng)該是undefined. 子組件是interface InterfaceProps {  onlyOneLeft?: boolean;  isEnabled?: boolean;  signInMethod?: any;  onUnlink?: any;  password?: string;  passwordTwo?: string;  handleFormSubmit?: any;  handleInputChange?: any;}const ChildComponent = ({ password }: InterfaceProps): any => {  const regUpCase = new RegExp('^(?=.*[A-Z])');  const regLwCase = new RegExp('^(?=.*[a-z])');  const regDigit = new RegExp('^(?=.*[0-9])');  const regChar = new RegExp('^(?=.*[*@!#%&()^~{}_-])');  const pwLength = password.length >= 8;  const pwUpCase = regUpCase.test(password);  const pwLwCase = regLwCase.test(password);  const pwChar = regChar.test(password);  const pwDigit = regDigit.test(password);  const pwSpecial = pwChar || pwDigit;  const isInvalid =    !pwLength || !pwUpCase || !pwLwCase || !pwSpecial || password === '';  return isInvalid ? (    ... // return when password isInvalid  ) : (    ... // return when password !isInvalid  );};ChildComponent.propTypes = {  password: PropTypes.string};export default ChildComponent;OnpwLength我看到了Object is possibly 'undefined'on 的錯(cuò)誤password prop。對(duì){ pwUpCase, pwLwCase, pwChar, pwDigit }在password prop我收到的錯(cuò)誤Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.在這種情況下,我的想法是props password永遠(yuǎn)不會(huì)undefined因?yàn)樗诟附M件中具有初始state值''。我是否仍然需要檢查password未定義?或者也許正確的方法應(yīng)該是移動(dòng)isInvalid到父組件?我寧愿我不需要,所以任何建議都會(huì)非常有幫助。
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

password 在您的界面上是可選的。

因此,您需要將默認(rèn)值傳遞給password.

喜歡:

const ChildComponent = ({ password = '' }: InterfaceProps): any => {
-----------------------------------^^^^


查看完整回答
反對(duì) 回復(fù) 2021-07-15
  • 1 回答
  • 0 關(guān)注
  • 759 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)