Skip to content

Incorrect type inference with indexer leads to TS2322 #28602

Closed

Description

@Manish3177

TypeScript Version: 3.1.1

Search Terms: Incorrect type inference, TS2322

class One
{
}
class Two
{
	public prop: One | null = null;
}
const aTwo = new Two();
aTwo.prop = null;	// makes TypeScript infer that the type of aTwo.prop is null but only when using indexer syntax which...
aTwo["prop"] = new One();	// results in TS2322: Type 'One' is not assignable to type null on this line
aTwo.prop = new One();	// this line is however fine

Also...

aTwo.prop = null; 	// commenting this out or
// aTwo.prop = new One();	// uncommenting this (when it's place before)
aTwo["prop"] = new One();	// makes the TS2322 error on this line go away

Expected behavior: aTwo["prop"] = new One(); should not result in an error

Actual behavior: It results in TS2322.

Playground Link:

Related Issues: The title of this sounds similar to #17011 but this is different and this is new in TypeScript 3.1 (AFAICT) and wasn't an issue in TypeScript 3.0.

Activity

  1. ajafff commented on Nov 19, 2018

    @ajafff
    Contributor

    Should already be fixed by #27574 in 3.2.0-rc

  2. Manish3177 commented on Nov 19, 2018

    @Manish3177
    Author

    Ah.. thanks for the instantaneous reply. Yes, this does sound like a duplicate of #27412 but unfortunately, its title didn't have the keywords I looked for.

  3. typescript-bot commented on Dec 12, 2018

    @typescript-bot
    Contributor

    This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions