1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-17 14:08:01 +00:00

Compare commits

..

3 Commits

4 changed files with 112 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1238 TILs and counting..._
_1241 TILs and counting..._
---
@@ -344,6 +344,7 @@ _1238 TILs and counting..._
- [Grab Multiple Values From A Reference](groq/grab-multiple-values-from-a-reference.md)
- [Grab Values From An Array Of References](groq/grab-values-from-an-array-of-references.md)
- [Include Attributes When Conditional Check Passes](groq/include-attributes-when-conditional-check-passes.md)
- [Include Type Of Operation In Webhook Response](groq/include-type-of-operation-in-webhook-response.md)
### Heroku
@@ -384,6 +385,7 @@ _1238 TILs and counting..._
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
- [Check Classes On A DOM Element](javascript/check-classes-on-a-dom-element.md)
- [Check If A Number Is Positive Or Negative](javascript/check-if-a-number-is-positive-or-negative.md)
- [Check If An Object Is Empty With Zod](javascript/check-if-an-object-is-empty-with-zod.md)
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
- [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md)
- [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md)
@@ -1141,6 +1143,7 @@ _1238 TILs and counting..._
- [Extract Object Type Values Into A Union Type](typescript/extract-object-type-values-into-a-union-type.md)
- [Generate Inferred Type From Zod Schema](typescript/generate-inferred-type-from-zod-schema.md)
- [Interfaces With The Same Name Are Merged](typescript/interfaces-with-the-same-name-are-merged.md)
- [Narrow The Type Of An Array To Its Values](typescript/narrow-the-type-of-an-array-to-its-values.md)
- [Re-Export An Imported Type](typescript/re-export-an-imported-type.md)
- [Type Narrowing With Const VS Let Strings](typescript/type-narrowing-with-const-vs-let-strings.md)
- [Type Narrowing With Similarly Shaped Objects](typescript/type-narrowing-with-similarly-shaped-objects.md)

View File

@@ -0,0 +1,40 @@
# Include Attributes When Conditional Check Passes
The graph-like nature of Sanity means that you'll often be querying for data
that spans a variety of document types. For instance, your front-end could be
requesting content in the shape of a blog post, video, and podcast. Though
there are some similarities, each of these document types will have some unique
attributes.
When using GROQ to query for data that spans different types of documents, you
can use [a conditional query
syntax](https://www.sanity.io/docs/query-cheat-sheet#64a36d80be73) to include
type-specific attributes.
```groq
*[_type == 'blog' || _type == 'video' || _type == 'podcast'][]{
title,
'slug': slug.current,
_type == 'blog' => {
body,
read_time
},
_type == 'video' => {
description,
mp4_url
},
_type == 'podcast' => {
description,
mp3_url
}
}
```
Notice that there are some attributes that are common across each type (i.e.
`title` and `slug`). Each type then has attributes unique to its document type
(i.e. `blog` has `body` and `read_time`).
This conditional query syntax allows us to both define flexible schemas in
Sanity and then query against that flexible schema.
[source](https://www.youtube.com/watch?v=dCGPNkcTseQ)

View File

@@ -0,0 +1,29 @@
# Check If An Object Is Empty With Zod
Zod is a schema validation library. It can be used to check all sorts of
properties about the data moving through our system.
Let's look at how to implement a common type of check -- is this object empty?
```javascript
import {z} from 'zod';
const emptyObjectSchema = z.object({}).strict();
const isEmpty = (obj: object): boolean => {
const result = emptyObjectSchema.safeParse(obj);
return result.success;
}
isEmpty({});
//=> true
isEmpty({ hello: 'world' });
//=> false
```
This `emptyObjectSchema` _strictly_ defines the schema as an empty object
(`{}`). Without the [`strict()`](https://github.com/colinhacks/zod#strict)
part, we'd be allowing an object with key-value pairs to quietly pass the
validation.
[source](https://twitter.com/jbrancha/status/1565728882082385920)

View File

@@ -0,0 +1,39 @@
# Narrow The Type Of An Array To Its Values
When an array of string values is defined in a TypeScript context, the inferred
type will be `string[]`. That's because the values of an array can be
reassigned. The most precise TypeScript can be is to say that it is an array of
string.
```typescript
const actions = ['increase', 'decrease'];
// typeof actions => string[]
```
We can freeze the array with its values using `as const`, the [const
assertion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions).
This doesn't actually _freeze_ the array, but it does mark it as `readonly` for
the TypeScript compiler.
That means we can lean on the compiler for a lot more specific feedback.
Consider for instance this `reducer` function.
```typescript
const actions = ['increase', 'decrease'] as const
const reducer = (action: typeof actions[number]) => {
switch (action) {
case 'increase':
// do an increase
case 'decrease':
// do a decrease
case 'submit': // TYPE ERROR, "submit" is not comparable to type 'increase' | 'decrease'
// do a submit
default:
throw Error('Unrecognized action!')
}
}
// TYPE ERROR, "submit" is not comparable to type 'increase' | 'decrease'
reducer('submit')
```