mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Don't allow everything to be assignable to string within string mappings like Uppercase/Lowercase (#52734)
This commit is contained in:
parent
5b71f59450
commit
79df2bcd35
@ -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) {
|
||||
|
||||
@ -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>'.
|
||||
|
||||
9
tests/baselines/reference/stringMappingAssignability.js
Normal file
9
tests/baselines/reference/stringMappingAssignability.js
Normal 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" };
|
||||
10
tests/baselines/reference/stringMappingAssignability.symbols
Normal file
10
tests/baselines/reference/stringMappingAssignability.symbols
Normal 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))
|
||||
|
||||
11
tests/baselines/reference/stringMappingAssignability.types
Normal file
11
tests/baselines/reference/stringMappingAssignability.types
Normal 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"
|
||||
|
||||
4
tests/cases/compiler/stringMappingAssignability.ts
Normal file
4
tests/cases/compiler/stringMappingAssignability.ts
Normal file
@ -0,0 +1,4 @@
|
||||
// @strict: true
|
||||
|
||||
const x: Uppercase<string> = 42;
|
||||
const y: Uppercase<string> = { foo: "bar" };
|
||||
Loading…
x
Reference in New Issue
Block a user