1.0.0 • Published 7 months ago

null-proto-ts v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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}

Links