Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression in nightly build related to keyof, type narrowing, and exactOptionalPropertyTypesΒ #55756

Closed as not planned
@mtreder

Description

@mtreder

πŸ”Ž Search Terms

keyof
type narrowing
exactOptionalPropertyTypes

πŸ•— Version & Regression Information

  • This is newly identified as error by the compiler
  • This changed between versions nightly build on 9/13 and 9/14
  • This is the behavior in every version I tried prior to Nightly

⏯ Playground Link

https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true&ts=5.3.0-dev.20230915#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgSQCFMBnPAbwFgAoASAH0AjKTJAEwC45WBPAbhoBfGjVCRYiFGiy4CAETQIAbsDYBBOKFTsSBYmThU6YKBDBoYPNQH4ujCBAA2wVgOrDqo8NHjJUGHDx8BShlVUJNEG02XSJSChpaEzMLHkJbOBIYUKQAczcPL3F4S3MCNUdHAHkwGAQIJF0AXj14uAAyRODFFXUOrpCwtkI3Ip84dABXJGw6hrhsRwbgGrnGgApEkihsLnwK6tr6xoAaRLZgLLgW4ABbWp5V45IAHn3Kp4aSAD51gEoztQ-oZEusqowAFbAWYAOgA1sAeCR1ttsMDSHB1gieBB0OUPkcvn8ANoAXT+MPQ0AAojgABbrMDXb4gui0BB49alYC4zI7YlgUlwACETRaACIIJDoTBxcCjLRFRcsgKhS1Uaq3IqPLRBH9RnQoMAYJMoEg4MqYAURNQpjM1pp7pZPo0XgAVSLRWIHF0-f5cD0Ko0ms2GOBMFjsLhKCAINhwAAMcEE3F0boKQA

πŸ’» Code

// @strict: true
// @exactOptionalPropertyTypes: true

export interface IBase {
	_brand: any;
}

export interface IDerivedA extends IBase {
	propertyA?: boolean;
}

export interface IDerivedB extends IBase {
	propertyB?: string;
}

export type IAllOptions = IBase &
	IDerivedA &
	IDerivedB;

export function cloneOptions(
	src: IAllOptions,
	dest = emptyOptions<IAllOptions>(),
) {
	(Object.keys(src) as (keyof IAllOptions)[]).forEach(p => {
		if (typeof src[p] !== "object") {
			dest[p] = src[p];
		}
	});

	return dest;
}

function emptyOptions<T extends IAllOptions>(): T {
	return { _brand: void 0 } as T;
}

πŸ™ Actual behavior

There is an error on the line attempting to copy src[p] into dest[p]:

type 'any' is not assignable to type 'never'.

πŸ™‚ Expected behavior

Previously this compiled without error. Both objects are of the same type, so they should have the same properties.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions