#Updating first letter of a string... Type error

1 messages · Page 1 of 1 (latest)

junior yew
#

I'm following along with code from this Stack Overflow comment -> https://stackoverflow.com/a/71098593/15592981

I'm following up to a certain point, but when the code creates the capitalizedKey variable, things start to break and I get three main errors.

  1. Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'keyof OptionalConfig'. Property '0' does not exist on type 'keyof OptionalConfig'.ts(7053)
  2. Property 'substring' does not exist on type 'keyof OptionalConfig'. Property 'substring' does not exist on type 'number'.ts(2339)
  3. Type 'K' does not satisfy the constraint 'string'. Type 'keyof OptionalConfig' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.ts(2344)

https://tsplay.dev/Nap4Bw

Although I've modified the code to use a simpler useMediaQuery hook, they both return booleans and should't effect the overall structure.

Also what is the 'Capitalize' in is${Capitalize<K>} referring to?

silent oracleBOT
#
that_guy977#0

Preview:```ts
import {useMediaQuery} from "./useMediaQuery" // returns boolean

import resolveConfig from "tailwindcss/resolveConfig"
import tailwindConfig from "@/tailwind.config"

const breakpoints = resolveConfig(tailwindConfig)

type BreakpointKey = keyof typeof breakpoints & string
...```

wintry widget
#

keyof can also give number and symbol

junior yew
#

Thanks for that. There's so much to take on TypeScript. I'll deffo read up on keyof.

Thanks Chris 🙏

#

!resolved

wintry widget
#

completely missed your second question, just noticed browsing old threads