Skip to content

Ignore __proto__ #13933

Open
Open

Description

@Riim

TypeScript Version: 2.2.0

Code:

{ __proto__: {} } as { [key: string]: string }

Expected behavior:
Ok

Actual behavior:
Type '{ __proto__: {}; }' cannot be converted to type '{ [key: string]: string; }'.

Activity

  1. felixfbecker commented on Feb 10, 2017

    @felixfbecker
    Contributor

    This is extremely annoying. Consider this

    export const stepComponent = {
        bindings: {
            hasNextStep: '>',
            hasPreviousStep: '>',
            onNextStep: '&',
            onPreviousStep: '&'
        },
        controller: class StepController {
            public hasNextStep: boolean;
            public hasPreviousStep: boolean;
            public onNextStep: Function;
            public onPreviousStep: Function;
        }
    };
    
    angular.component('credentialsStep', {
        __proto__: stepComponent,
        bindings: {
            __proto__: stepComponent.bindings,
            user: '>'
        },
        controller: class CredentialsStepController extends stepComponent.controller {
    
            public credentialsForm: angular.IFormController;
            public user: any;
            public repeatedPassword: string;
        }
    });

    The credentialsComponent should be valid and have all properties of the stepComponent, instead it fails with

    [ts]
    Argument of type '{ __proto__: { bindings: { hasNextStep: string; hasPreviousStep: string; onNextStep: string; onPr...' is not assignable to parameter of type 'IComponentOptions'.
      Object literal may only specify known properties, and '__proto__' does not exist in type 'IComponentOptions'.
    
  2. jolexxa commented on Mar 6, 2019

    @jolexxa

    Also ran into this tonight while making custom error subclasses for Node. Was expecting TypeScript to ignore anything relating to .__proto__ or at least have a mechanism for avoiding it.

    class CustomError extends Error {
    	constructor (message?: string) {
    		// 'Error' breaks prototype chain here
    		super(message);
    
    		// restore prototype chain
    		const actualProto = new.target.prototype;
    
    		if (Object.setPrototypeOf) { Object.setPrototypeOf(this, actualProto); } else { this.__proto__ = actualProto; }
    	}
    }

    Credits:
    https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget

    https://stackoverflow.com/a/48342359

  3. exedor commented on May 31, 2020

    @exedor

    Same here. I just ran into this using the following loop structure:

    for (let i in qList) { qList[i].prop1.trim(); qList[i].prop2.trim(); }

    It is a simple array of objects. The job of the loop is to iterate over all the objects and trim two of it's string properties. The documentation for typescript talks about iterating over the indexes of the array. The problem is after it's done doing that, the value of i changes from an index to 'contains' which is method on

    __proto__

    This is completely broke our application when we upgraded typescript to 2.9.2. It worked great on 1.8.

  4. mike-marcacci commented on Oct 20, 2021

    @mike-marcacci

    Just as a note, a full solution for this is described in this RFC: #38385.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions