Don't allow everything to be assignable to string within string mappings like Uppercase/Lowercase (#52734)

This commit is contained in:
Jake Bailey 2023-02-13 17:58:56 -05:00 committed by GitHub
parent 5b71f59450
commit 79df2bcd35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 2 deletions

View File

@ -23875,10 +23875,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function isMemberOfStringMapping(source: Type, target: Type): boolean {
if (target.flags & (TypeFlags.String | TypeFlags.Any)) {
if (target.flags & TypeFlags.Any) {
return true;
}
if (target.flags & TypeFlags.TemplateLiteral) {
if (target.flags & (TypeFlags.String | TypeFlags.TemplateLiteral)) {
return isTypeAssignableTo(source, target);
}
if (target.flags & TypeFlags.StringMapping) {

View File

@ -0,0 +1,12 @@
tests/cases/compiler/stringMappingAssignability.ts(1,7): error TS2322: Type 'number' is not assignable to type 'Uppercase<string>'.
tests/cases/compiler/stringMappingAssignability.ts(2,7): error TS2322: Type '{ foo: string; }' is not assignable to type 'Uppercase<string>'.
==== tests/cases/compiler/stringMappingAssignability.ts (2 errors) ====
const x: Uppercase<string> = 42;
~
!!! error TS2322: Type 'number' is not assignable to type 'Uppercase<string>'.
const y: Uppercase<string> = { foo: "bar" };
~
!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'Uppercase<string>'.

View File

@ -0,0 +1,9 @@
//// [stringMappingAssignability.ts]
const x: Uppercase<string> = 42;
const y: Uppercase<string> = { foo: "bar" };
//// [stringMappingAssignability.js]
"use strict";
var x = 42;
var y = { foo: "bar" };

View File

@ -0,0 +1,10 @@
=== tests/cases/compiler/stringMappingAssignability.ts ===
const x: Uppercase<string> = 42;
>x : Symbol(x, Decl(stringMappingAssignability.ts, 0, 5))
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
const y: Uppercase<string> = { foo: "bar" };
>y : Symbol(y, Decl(stringMappingAssignability.ts, 1, 5))
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
>foo : Symbol(foo, Decl(stringMappingAssignability.ts, 1, 30))

View File

@ -0,0 +1,11 @@
=== tests/cases/compiler/stringMappingAssignability.ts ===
const x: Uppercase<string> = 42;
>x : Uppercase<string>
>42 : 42
const y: Uppercase<string> = { foo: "bar" };
>y : Uppercase<string>
>{ foo: "bar" } : { foo: string; }
>foo : string
>"bar" : "bar"

View File

@ -0,0 +1,4 @@
// @strict: true
const x: Uppercase<string> = 42;
const y: Uppercase<string> = { foo: "bar" };