Skip to content

Argument of type 'string | String' is not assignable to parameter of type 'string'.Β #63073

@kkmuffme

Description

@kkmuffme

πŸ”Ž Search Terms

"string|String" primitive object collapse

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/FDBmFcDsGMBcEsD2kAEpGIBQoLYFMBnAgQwHM8AuFKAa0kQHdUBKFAb2AEhP5QVMunbAEJ+uQiXIp4kArGIw8iPgGVYAJxmkUrAGS6UsAJ4AHJX3xEyeFMIC8dlAHI5myKSc6UAH2+DRAPTEAEbQACYBAHSwhLDYlpI2zFysHFwAvsCZIMBAA

πŸ’» Code

function foo( message: unknown ) {
		if (
		( ! ( message instanceof String ) && typeof message !== 'string' ) ||
		! /abcd/.test( message )
	) {

	}
}

πŸ™ Actual behavior

Argument of type 'string | String' is not assignable to parameter of type 'string'.
Type 'String' is not assignable to type 'string'.
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

πŸ™‚ Expected behavior

No error, should be collapsed, since String can be used as string in .test()

Additional information about the issue

Maybe related to #22617 ?

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