Skip to content

Incorrect type inference with indexer leads to TS2322 #28602

Closed
@Manish3177

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.

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