Skip to content

Remove incorrect handling of intersections in getStringMappingType #53383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17113,7 +17113,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function getStringMappingType(symbol: Symbol, type: Type): Type {
return type.flags & (TypeFlags.Union | TypeFlags.Never) ? mapType(type, t => getStringMappingType(symbol, t)) :
type.flags & TypeFlags.Intersection ? getIntersectionType(map((type as IntersectionType).types, t => getStringMappingType(symbol, t))) :
type.flags & TypeFlags.StringLiteral ? getStringLiteralType(applyStringMapping(symbol, (type as StringLiteralType).value)) :
type.flags & TypeFlags.TemplateLiteral ? getTemplateLiteralType(...applyTemplateStringMapping(symbol, (type as TemplateLiteralType).texts, (type as TemplateLiteralType).types)) :
// Mapping<Mapping<T>> === Mapping<T>
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/templateLiteralIntersection3.types
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ options1[`foo/${path}`] = false;

// Lowercase<`foo/${Path}`> => `foo/${Lowercase<Path>}`
declare const lowercasePath: Lowercase<`foo/${Path}`>;
>lowercasePath : `foo/${Lowercase<string> & { _pathBrand: any; }}`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was wrong in #52836; the intersection here should not be getting lifted out of the string mapping.

>lowercasePath : `foo/${Lowercase<`${Path}`>}`

options1[lowercasePath] = false;
>options1[lowercasePath] = false : false
>options1[lowercasePath] : boolean
>options1 : { prop: number; } & { [k: string]: boolean; }
>lowercasePath : `foo/${Lowercase<string> & { _pathBrand: any; }}`
>lowercasePath : `foo/${Lowercase<`${Path}`>}`
>false : false

24 changes: 24 additions & 0 deletions tests/baselines/reference/templateLiteralIntersection4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [templateLiteralIntersection4.ts]
type StateHook<S> = () => [S, unknown];

type StoreUtils<Store extends { [K: string]: any }> = Omit<{
[K in keyof Store as `use${Capitalize<string & K>}`]: StateHook<Store[K]>
}, 'useStore'> & {
Provider: unknown,
useStore: StateHook<Store>
};

declare function createStore<Store extends { [K: string]: any }>(store: Store): StoreUtils<Store>;

const { Provider, useUsername, useAge, useStore } = createStore({
username: "Aral",
age: 31
});


//// [templateLiteralIntersection4.js]
"use strict";
var _a = createStore({
username: "Aral",
age: 31
}), Provider = _a.Provider, useUsername = _a.useUsername, useAge = _a.useAge, useStore = _a.useStore;
56 changes: 56 additions & 0 deletions tests/baselines/reference/templateLiteralIntersection4.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=== tests/cases/compiler/templateLiteralIntersection4.ts ===
type StateHook<S> = () => [S, unknown];
>StateHook : Symbol(StateHook, Decl(templateLiteralIntersection4.ts, 0, 0))
>S : Symbol(S, Decl(templateLiteralIntersection4.ts, 0, 15))
>S : Symbol(S, Decl(templateLiteralIntersection4.ts, 0, 15))

type StoreUtils<Store extends { [K: string]: any }> = Omit<{
>StoreUtils : Symbol(StoreUtils, Decl(templateLiteralIntersection4.ts, 0, 39))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 2, 16))
>K : Symbol(K, Decl(templateLiteralIntersection4.ts, 2, 33))
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))

[K in keyof Store as `use${Capitalize<string & K>}`]: StateHook<Store[K]>
>K : Symbol(K, Decl(templateLiteralIntersection4.ts, 3, 5))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 2, 16))
>Capitalize : Symbol(Capitalize, Decl(lib.es5.d.ts, --, --))
>K : Symbol(K, Decl(templateLiteralIntersection4.ts, 3, 5))
>StateHook : Symbol(StateHook, Decl(templateLiteralIntersection4.ts, 0, 0))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 2, 16))
>K : Symbol(K, Decl(templateLiteralIntersection4.ts, 3, 5))

}, 'useStore'> & {
Provider: unknown,
>Provider : Symbol(Provider, Decl(templateLiteralIntersection4.ts, 4, 18))

useStore: StateHook<Store>
>useStore : Symbol(useStore, Decl(templateLiteralIntersection4.ts, 5, 20))
>StateHook : Symbol(StateHook, Decl(templateLiteralIntersection4.ts, 0, 0))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 2, 16))

};

declare function createStore<Store extends { [K: string]: any }>(store: Store): StoreUtils<Store>;
>createStore : Symbol(createStore, Decl(templateLiteralIntersection4.ts, 7, 2))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 9, 29))
>K : Symbol(K, Decl(templateLiteralIntersection4.ts, 9, 46))
>store : Symbol(store, Decl(templateLiteralIntersection4.ts, 9, 65))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 9, 29))
>StoreUtils : Symbol(StoreUtils, Decl(templateLiteralIntersection4.ts, 0, 39))
>Store : Symbol(Store, Decl(templateLiteralIntersection4.ts, 9, 29))

const { Provider, useUsername, useAge, useStore } = createStore({
>Provider : Symbol(Provider, Decl(templateLiteralIntersection4.ts, 11, 7))
>useUsername : Symbol(useUsername, Decl(templateLiteralIntersection4.ts, 11, 17))
>useAge : Symbol(useAge, Decl(templateLiteralIntersection4.ts, 11, 30))
>useStore : Symbol(useStore, Decl(templateLiteralIntersection4.ts, 11, 38))
>createStore : Symbol(createStore, Decl(templateLiteralIntersection4.ts, 7, 2))

username: "Aral",
>username : Symbol(username, Decl(templateLiteralIntersection4.ts, 11, 65))

age: 31
>age : Symbol(age, Decl(templateLiteralIntersection4.ts, 12, 19))

});

42 changes: 42 additions & 0 deletions tests/baselines/reference/templateLiteralIntersection4.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/templateLiteralIntersection4.ts ===
type StateHook<S> = () => [S, unknown];
>StateHook : StateHook<S>

type StoreUtils<Store extends { [K: string]: any }> = Omit<{
>StoreUtils : StoreUtils<Store>
>K : string

[K in keyof Store as `use${Capitalize<string & K>}`]: StateHook<Store[K]>
}, 'useStore'> & {
Provider: unknown,
>Provider : unknown

useStore: StateHook<Store>
>useStore : StateHook<Store>

};

declare function createStore<Store extends { [K: string]: any }>(store: Store): StoreUtils<Store>;
>createStore : <Store extends { [K: string]: any; }>(store: Store) => StoreUtils<Store>
>K : string
>store : Store

const { Provider, useUsername, useAge, useStore } = createStore({
>Provider : unknown
>useUsername : StateHook<string>
>useAge : StateHook<number>
>useStore : StateHook<{ username: string; age: number; }>
>createStore({ username: "Aral", age: 31}) : StoreUtils<{ username: string; age: number; }>
>createStore : <Store extends { [K: string]: any; }>(store: Store) => StoreUtils<Store>
>{ username: "Aral", age: 31} : { username: string; age: number; }

username: "Aral",
>username : string
>"Aral" : "Aral"

age: 31
>age : number
>31 : 31

});

17 changes: 17 additions & 0 deletions tests/cases/compiler/templateLiteralIntersection4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @strict: true

type StateHook<S> = () => [S, unknown];

type StoreUtils<Store extends { [K: string]: any }> = Omit<{
[K in keyof Store as `use${Capitalize<string & K>}`]: StateHook<Store[K]>
}, 'useStore'> & {
Provider: unknown,
useStore: StateHook<Store>
};

declare function createStore<Store extends { [K: string]: any }>(store: Store): StoreUtils<Store>;

const { Provider, useUsername, useAge, useStore } = createStore({
username: "Aral",
age: 31
});