null-proto-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Null Prototypes (TypeScript)

These are types that makes TypeScript report sane errors when trying to access properties defined in Object.prototype on objects with a null-prototype.

Usage

Basic Usage

The basic types with defined properties behaves correctly

import NullProto from "null-proto-ts";


// Correct behavior when toString is missing:
const objWithTyping: NullProto<{a?: string; b?: number}> = Object.create(null);

console.log(objWithTyping.a);
// Works as expected

console.log(objWithTyping.b);
// Works as expected

console.log(objWithTyping.toString()); 
// Gives expected typescript error:
// Property 'toString' does not exist on type 'NullProto<{ a?: string | undefined; b?: number | undefined; }>'.ts(2339)

// Incorrect behavior when toString is missing:
const objWithoutTyping: {a?: string; b?: number} = Object.create(null);

console.log(objWithoutTyping.a);
// Works as expected

console.log(objWithoutTyping.b);
// Works as expected

console.log(objWithoutTyping.toString()); 
// Erroneously gives no errors. Typescript makes all properties from Object.prototype accessible from the type {a?: string; b?: number}

Mapped keys

Mapped keys are a bit tricky, due to how you want the Object.prototype keys to be accessible with the correct mapped type.

import NullProto from "null-proto-ts";

// Correct behavior when having a mapped key:
const objWithTyping: NullProto<{[k: string]: string}> = Object.create(null);

console.log(objWithTyping.anyNormalKey);
// Works as expected

console.log(objWithTyping.anyOtherNormalKey);
// Works as expected

console.log(objWithTyping.toString()); 
// Gives expected typescript error:
// This expression is not callable.
//  Type 'String' has no call signatures.ts(2349)

// Incorrect behavior when having mapped keys:
const objWithoutTyping: {[k: string]: string} = Object.create(null);

console.log(objWithTyping.anyNormalKey);
// Works as expected

console.log(objWithTyping.anyOtherNormalKey);
// Works as expected

console.log(objWithoutTyping.toString()); 
// Erroneously gives no errors. Typescript makes the properties from Object.prototype take priority over the mapped key in {[k: string]: string}

Package Sidebar

Install

npm i null-proto-ts

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

63.4 kB

Total Files

7

Last publish

Collaborators

  • simplylinn