Skip to content

Property does not exist on type 'never' reported when it shouldn't #27041

@hanginwithdaddo

Description

@hanginwithdaddo

TypeScript Version: 3.0.3

Search Terms:
property does not exist on type 'never'

The code fragment below without any changes produces the following error when
using TypeScript 3.0.3/TSLint 5.11.0:

(44,47): Property 'b' does not exist on type 'never'.
Replacing the "if isNotDefined(options)" line in ClassB.modify() with either of the lines above it that are commented out stops this error from appearing.

Code fragment:

export function myIsDefined<T>(value: T | undefined | null): value is T {
  return <T>value !== undefined && <T>value !== null;
}

export function myIsNotDefined<T>(value: T | undefined | null): value is T {
	return <T>value === undefined || <T>value === null;
}

export interface ClassAModifyResult {
	a: boolean
}

export interface ClassBModifyResult extends ClassAModifyResult {
	b: boolean
}

export class ClassA {
	constructor(options?: any) {
		this.modify(options);
	}

	public modify(options?: any): ClassAModifyResult {
		var result = {} as ClassAModifyResult;
		if ( myIsNotDefined(options) )
			return result;

		result.a = true;
		return result;
	}
}

export class ClassB extends ClassA {
	constructor(options?: any) {
		super(options);
	}

	public modify(options?: any): ClassBModifyResult {
		// if ( !myIsDefined(options) )
		// if ( options === undefined || options === null )
		if ( myIsNotDefined(options) )
			return {} as ClassBModifyResult;

		var result = super.modify(options) as ClassBModifyResult;
		result.b = this._modifyBIfNecessary(options.b);
		return result;
	}

	protected _modifyBIfNecessary(value?: boolean): boolean {
		return true;
	}
}

I have seen other cases where using a method like myIsNotDefined() causes other issues for either TypeScript or TSLint.

Example:

	someMethod(value?: any | null): void {
		if ( myIsNotDefined(value) )
			return;

		if ( value.member === 'abc' )
			doSomething();
	}

With the code fragment above an error may appear stating that 'value' maybe undefined or null at the line that reads "if ( value.member === 'abc' )". Replacing the call to 'myIsNotDefined(value)' with either '!myIsDefined(value)' or 'value === undefined || value === null' stops the error from appearing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions