How to override a property to be non-nullable in Typescript
Asked Answered
E

5

44

The DefinitelyTyped definition of the Node built-in IncomingMessage (the type of req in the (req, res, next) arguments) has defined url to be nullable. Here's the snipped parts of the definition file:

// @types/node/index.d.ts
declare module "http" {
  export interface IncomingMessage {
    /**
     * Only valid for request obtained from http.Server.
     */
    url?: string;
  }
}

As the comment says, this is because this property is only valid when you're getting an instance of this IncomingMessage from the http.Server. In other uses it won't exist, hence, it's nullable.

However, in my case, I know that I'm only getting these instances from http.Server, and so it's kinda annoying that I can't just access the property without extra guards.

import { IncomingMessage, ServerResponse } from 'http';

function someMiddleware(req: IncomingMessage, res: ServerResponse, next: Function) {
  const myStr: string = req.url; // bzzzt.
  // Argument of type 'string | undefined' is not
  // assignable to parameter of type 'string'.
}

It's probably good to mention that I'm using TS 2.0.3 with strictNullChecks, which is not enabled on the Typescript Playground.

Here's the question. Is it possible to override that definition across my application so that url is not nullable?


Here's what I've already tried... adding this to one of my files:

declare module 'http' {
  interface IncomingMessage {
    url: string;
  }
}

...however that is disallowed: "Subsequent variable declarations must have the same type". This is explained in the documentation.

The only thing I can think of thus far is to create my own module which imports, extends and then exports the interfaces:

// /src/http.ts
import { IncomingMessage as OriginalIM } from 'http';
export interface IncomingMessage extends OriginalIM {
  url: string;
}

// src/myapp.ts
import { IncomingMessage } from './http'; // <-- local def

function someMiddleware(req: IncomingMessage) {
  const str: string = req.url; // all good
}

So, this works, but it seems so wrong.

Ethelstan answered 1/11, 2016 at 23:30 Comment(1)
IMO what you did is not wrong provided you are sure this object property is always present. Certainly better than casting this type everywhere in the code.Pochard
A
41

As of TypeScript 2.1, you can use a lookup type to access an interface property.

IncomingMessage['url'] // string | undefined

You can combine that with NonNullable to fit your use case.

NonNullable<IncomingMessage['url']> // string

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html

African answered 24/1, 2019 at 16:19 Comment(0)
E
26

So I found a solution which is slightly less hacky.

TypeScript 2.0 also has added a non-null assertion operator: !

function someMiddleware(req: IncomingMessage) {
  const str1: string = req.url;  // error, can't assign string | undefined to string
  const str2: string = req.url!; // works
}

In my case, it's still a bit annoying, since there are many different files which need to access this property and so this non-null assertion is used in many places.

Ethelstan answered 2/11, 2016 at 0:0 Comment(2)
Thank you for pointing it out! I only add for others that may not follow your link, that the ! operator asserts both non-null and non-undefined.Waltman
This operator should be used only in contexts where the type checker is unable to conclude that fact, for because of the business logic. Using it everywhere as a rule can be confusing.Pochard
P
26

In your sample case, it's easy because you want to get rid of ALL undefined, therefore use the Required utility type.

interface IncomingMessage { url?: string; }
type ValidMessage = Required<IncomingMessage>;

ValidMessage will have all properties required.

But for those coming here to find out how to get rid of ALL null, you can use this custom utility type.

export type NonNullableFields<T> = {
  [P in keyof T]: NonNullable<T[P]>;
};

interface IncomingMessage { url: string | null; }
type ValidMessage = NonNullableFields<IncomingMessage>;

ValidMessage will have all properties not null.

And for those coming here to find out how to get rid of null only for specific fields, you can use these custom utility types.

export type NonNullableFields<T> = {
  [P in keyof T]: NonNullable<T[P]>;
};

export type NonNullableField<T, K extends keyof T> = T &
NonNullableFields<Pick<T, K>>;

interface IncomingMessage { url: string | null; }
type ValidMessage = NonNullableField<IncomingMessage, 'url'>;

ValidMessage will have the property url not null.

Pun answered 22/7, 2022 at 16:31 Comment(0)
W
10

Here's a solution defining a utility type RequiredProperties:

type RequiredProperties<T, P extends keyof T> = Omit<T, P> & Required<Pick<T, P>>;

Example usage:

type Foo = {
    a?: any;
    b?: any;
    c?: any;
};

type Bar = RequiredProperties<Foo, 'a' | 'b'>;

const bar1: Bar = { a: 1, b: 2, c: 3 };
const bar2: Bar = { b: 2, c: 3 }; // fails because `a` is now required
const bar3: Bar = { c: 3 }; // fails because both `a` and `b` are missing
Waldenburg answered 22/4, 2022 at 22:47 Comment(0)
B
0

If you need to exclude null | undefined props from some object you can use:

type RequiredProps<T, P extends keyof T> = Omit<T, P> & Required<Pick<T, P>> & Record<P, NonNullable<T[P]>>;

For example:

interface ISome {
    p1: string;
    p2?: string | null;
    p3?: number;
}

const withRequiredProps: RequiredProps<ISome, 'p2' | 'p3'> = { p1: '1', p2: '2', p3: 3 } 
Benedict answered 23/6, 2024 at 10:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.