Trying to understand the purpose of keyword "number"

Hello I've been following the web tutorial that theo put out and Im at the part where he brings in a type from trpc and he defines that by putting in [number] at the end. I'm just confused on the significance of that keyword is that JavaScript thing. I have never seen it before. Im just looking for an explanation. Thanks
4 Replies
Sybatron
Sybatron2y ago
It's not js thing it's ts It's the indexed type access If you want to check for more https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html
Documentation - Indexed Access Types
Using Type['a'] syntax to access a subset of a type.
Gabriel
Gabriel2y ago
This is related to arrays. You see, when you call an array and want to get one item in it, you give out an index. Ex:
const arr = [1, 'Banana']
const secondValue = arr[1]
const arr = [1, 'Banana']
const secondValue = arr[1]
The [number] in typeProps you are seeing is like accessing the type of the array type Im bad at explaining but I hope it made sense
Sybatron
Sybatron2y ago
The number part in the end is for getting the post type if im correct From that line I'm guessing you get all posts as array and saying [number] you get the type of the posts inside this array
Gabriel
Gabriel2y ago
const asd: [string, object] = ["asd", { banana: "isAwesome" }];
type SecondTypeFromArray = (typeof asd)[1];

type TypeOfAnyArray = (typeof asd)[number];
const asd: [string, object] = ["asd", { banana: "isAwesome" }];
type SecondTypeFromArray = (typeof asd)[1];

type TypeOfAnyArray = (typeof asd)[number];
The SecondTypeFromArray will be an object. The TypeOfAnyArray will be string | object Also, yes. It's a typescript thing not javascript. All related to types
Want results from more Discord servers?
Add your server