next-seo icon indicating copy to clipboard operation
next-seo copied to clipboard

Allow null for typescript definitions

Open harvestnide opened this issue 2 years ago • 0 comments

Duplicate of #840 - which was closed by stale bot

Describe the bug With strict-null check enabled - you cannot use null as prop for any meta block. All my meta fields are coming from Static/ServerSide Props and there is a catch - next refuses to serialize undefined, meaning that I have to either filter out all undefined values from objects (and there is no easy way to do it recursively and type-safe), or replace them with 0 / empty string (depending on field type).

Reproduction

import type {  OpenGraphMedia } from 'next-set/lib/types';

/*
TS2322: Type 'number | null' is not assignable to type 'number | undefined'.   Type 'null' is not assignable to type 'number | undefined'.  
The expected type comes from property 'width' which is declared here on type 'OpenGraphMedia'
*/
const images: OpenGraphMedia = {
  url: img.sourceUrl,
  width: img.mediaDetails?.width || null,
  height: img.mediaDetails?.height || null,
};

Additional context I checked couple of files in this repo and it seems like value is implicitly typecasted to boolean, and null is falsy in a same way undefined is. Of course, some investigation is required, but it seems like an extremely easy fix.

harvestnide avatar Jul 13 '22 12:07 harvestnide