Stop Using {} In TypeScript

preview_player
Показать описание
🌎 Find Me Here:

#Shorts
Рекомендации по теме
Комментарии
Автор

Type-safe language programmers:

"Look what they need just to mimic a fraction of our power."

DrowsyBoi
Автор

I name it key instead of index.
{ [key:string] : unknown }

abhayprince
Автор

For a split second I read "Stop using TypeScript"

christianpuldon
Автор

also you can do `Record<PropertyKey, unknown>`
`PropertyKey` shorthand for `string | symbol | number`

nomadshiba
Автор

you should mention the `object` (note lowercase) this is the most generic pure object you can specify, usually useful it you're extended a generic. function <T extends object>foo (bar: T) { return Object.keys(bar) }

und
Автор

thanks kyle, keep with the snippets of TS

devperatetechno
Автор

Record<string, unknown> won't exclude arrays though.

If you want to exclude arrays, you'll need to do `Record<symbol, unknown>`.

There are a few small corner cases where `Record<symbol, unknown>` is too strict, but those don't come up much in user-land.

andrewjarrett
Автор

These shorts are awesome, goat js/ts content creator

ffuego
Автор

Me watching this even though I know nothing about coding: Very nice

RonnieMcNutt_Mindblowing
Автор

The title made me think for a moment there was a trick in TS to get rid of the block scope delimiters "{" "}" lol

thefanboy
Автор

please make more and more shorts. I love your content

darshitgajjar
Автор

Great shorts normally but I never bump into this issue, which is confusing as you say "by far the most common issue". If it's being passed like that its normally more defined like as an interface, or as you recommend a type. I never go near the Object type directly.

rtpHarry
Автор

{} is actually pretty powerful when you want to accept any value that isn’t undefined or null. It’s an any, but that any must exist

bob_kazamakis
Автор

BEWARE! Objects can ONLY HAVE STRINGS AS KEYS, so the types you specify here for keys are meaningless and can lead to very hard-to-find bugs. I wasted so many hours due to this. Just use Map<> instead

smitxp
Автор

Super helpful! Relaxes my brain to think of it this way

travispulley
Автор

Deno helped me a lot to learn TypeScript and avoid some bad practices such as this one

youarethecssformyhtml
Автор

One could just type it like this:
type Obj = {
name: string;
surname: string;
};

Record is only needed if you want to build a versatile function that handles different objects with the same structure or if you expect an object from an API f.ex. to change property names in the future

dimarak
Автор

I've been all up in Record<string, unknown> in the past months. It seems cleaner to my eyes for some reason than the key index approach (2nd version he showed).

AdamLeis
Автор

I use an Interface and mark each things as its type

instance objI {
name: string,
email: string,
image: string,
}

Is this wrong?

OliverEbsworth
Автор

More please. I'm transitioning to Typescript.

j.r.r.tolkien