最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - JavaScript optional chaining dynamic property - Stack Overflow

programmeradmin5浏览0评论

I am trying to access a dynamic property with safety provided by optional chaining that is available in TS. However it appears that this is not valid.

export const theme = {
  headers: {
    h1: {
    },
    h6: {
      color: '#828286'
    },
  },
}
console.info(theme?.headers?.['h6']?.color ?? '#000') //will pass
console.info(theme?.headers?.['h1']?.color ?? '#000') //will fail

Error

Identifier expected.  TS1003

    10 |   const StyledTypography = styled.div`
    11 |     margin: 0;
  > 12 |     color: #000; ${({theme}) => theme?.headers?.[variant]?.color ?? '#000'}
       |                                                ^
    13 |   `
    14 |   return (
    15 |     <StyledTypography as={variant}>

It appears that the optional changing will apply to the optional [] as a type but not to the values inside.

How can I make this optional without having to do [undefined || someDefaultValue]?

I am trying to access a dynamic property with safety provided by optional chaining that is available in TS. However it appears that this is not valid.

export const theme = {
  headers: {
    h1: {
    },
    h6: {
      color: '#828286'
    },
  },
}
console.info(theme?.headers?.['h6']?.color ?? '#000') //will pass
console.info(theme?.headers?.['h1']?.color ?? '#000') //will fail

Error

Identifier expected.  TS1003

    10 |   const StyledTypography = styled.div`
    11 |     margin: 0;
  > 12 |     color: #000; ${({theme}) => theme?.headers?.[variant]?.color ?? '#000'}
       |                                                ^
    13 |   `
    14 |   return (
    15 |     <StyledTypography as={variant}>

It appears that the optional changing will apply to the optional [] as a type but not to the values inside.

How can I make this optional without having to do [undefined || someDefaultValue]?

Share Improve this question edited Aug 29, 2020 at 2:01 Patrick Roberts 51.9k10 gold badges117 silver badges162 bronze badges asked Jan 10, 2020 at 13:41 Jamie HutberJamie Hutber 28.1k54 gold badges192 silver badges311 bronze badges 2
  • How exactly do you get that error, i can't reproduce it. – ASDFGerte Commented Jan 10, 2020 at 13:53
  • 2 You will need an environment where optional chaining is allowed. Then just use theme?.headers?.['h1']?.color ?? '#000' – Jamie Hutber Commented Jan 10, 2020 at 14:06
Add a comment  | 

1 Answer 1

Reset to default 17

You can create an interface that maps your theme object and pass the compiler type checking.

interface Headers {
    [key: string]: {
        [key: string]: string
    }
}

interface Theme {
    headers: Headers
}

const theme: Theme = {
  headers: {
    h1: {
    },
    h6: {
      color: '#828286'
    },
  },
}
console.info(theme?.headers?.['h6']?.color ?? '#000') //will pass
console.info(theme?.headers?.['h1']?.color ?? '#000') 
发布评论

评论列表(0)

  1. 暂无评论