-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Preserve the distributivity of inlined conditional types in declaration emit #48592
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
weswigham
merged 1 commit into
microsoft:main
from
weswigham:declaration-emit-conditional-wrapping
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/baselines/reference/declarationEmitInlinedDistributiveConditional.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//// [tests/cases/compiler/declarationEmitInlinedDistributiveConditional.ts] //// | ||
|
||
//// [test.ts] | ||
import {dropPrivateProps1, dropPrivateProps2} from './api'; | ||
const a = dropPrivateProps1({foo: 42, _bar: 'secret'}); // type is {foo: number} | ||
//a._bar // error: _bar does not exist <===== as expected | ||
const b = dropPrivateProps2({foo: 42, _bar: 'secret'}); // type is {foo: number, _bar: string} | ||
//b._bar // no error, type of b._bar is string <===== NOT expected | ||
|
||
//// [api.ts] | ||
import {excludePrivateKeys1, excludePrivateKeys2} from './internal'; | ||
export const dropPrivateProps1 = <Obj>(obj: Obj) => excludePrivateKeys1(obj); | ||
export const dropPrivateProps2 = <Obj>(obj: Obj) => excludePrivateKeys2(obj); | ||
|
||
//// [internal.ts] | ||
export declare function excludePrivateKeys1<Obj>(obj: Obj): {[K in PublicKeys1<keyof Obj>]: Obj[K]}; | ||
export declare function excludePrivateKeys2<Obj>(obj: Obj): {[K in PublicKeys2<keyof Obj>]: Obj[K]}; | ||
export type PublicKeys1<T> = T extends `_${string}` ? never : T; | ||
type PublicKeys2<T> = T extends `_${string}` ? never : T; | ||
|
||
//// [internal.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
//// [api.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.dropPrivateProps2 = exports.dropPrivateProps1 = void 0; | ||
var internal_1 = require("./internal"); | ||
var dropPrivateProps1 = function (obj) { return (0, internal_1.excludePrivateKeys1)(obj); }; | ||
exports.dropPrivateProps1 = dropPrivateProps1; | ||
var dropPrivateProps2 = function (obj) { return (0, internal_1.excludePrivateKeys2)(obj); }; | ||
exports.dropPrivateProps2 = dropPrivateProps2; | ||
//// [test.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
var api_1 = require("./api"); | ||
var a = (0, api_1.dropPrivateProps1)({ foo: 42, _bar: 'secret' }); // type is {foo: number} | ||
//a._bar // error: _bar does not exist <===== as expected | ||
var b = (0, api_1.dropPrivateProps2)({ foo: 42, _bar: 'secret' }); // type is {foo: number, _bar: string} | ||
//b._bar // no error, type of b._bar is string <===== NOT expected | ||
|
||
|
||
//// [internal.d.ts] | ||
export declare function excludePrivateKeys1<Obj>(obj: Obj): { | ||
[K in PublicKeys1<keyof Obj>]: Obj[K]; | ||
}; | ||
export declare function excludePrivateKeys2<Obj>(obj: Obj): { | ||
[K in PublicKeys2<keyof Obj>]: Obj[K]; | ||
}; | ||
export declare type PublicKeys1<T> = T extends `_${string}` ? never : T; | ||
declare type PublicKeys2<T> = T extends `_${string}` ? never : T; | ||
export {}; | ||
//// [api.d.ts] | ||
export declare const dropPrivateProps1: <Obj>(obj: Obj) => { [K in import("./internal").PublicKeys1<keyof Obj>]: Obj[K]; }; | ||
export declare const dropPrivateProps2: <Obj>(obj: Obj) => { [K in keyof Obj extends infer T ? T extends keyof Obj ? T extends `_${string}` ? never : T : never : never]: Obj[K]; }; | ||
//// [test.d.ts] | ||
export {}; |
76 changes: 76 additions & 0 deletions
76
tests/baselines/reference/declarationEmitInlinedDistributiveConditional.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
=== tests/cases/compiler/test.ts === | ||
import {dropPrivateProps1, dropPrivateProps2} from './api'; | ||
>dropPrivateProps1 : Symbol(dropPrivateProps1, Decl(test.ts, 0, 8)) | ||
>dropPrivateProps2 : Symbol(dropPrivateProps2, Decl(test.ts, 0, 26)) | ||
|
||
const a = dropPrivateProps1({foo: 42, _bar: 'secret'}); // type is {foo: number} | ||
>a : Symbol(a, Decl(test.ts, 1, 5)) | ||
>dropPrivateProps1 : Symbol(dropPrivateProps1, Decl(test.ts, 0, 8)) | ||
>foo : Symbol(foo, Decl(test.ts, 1, 29)) | ||
>_bar : Symbol(_bar, Decl(test.ts, 1, 37)) | ||
|
||
//a._bar // error: _bar does not exist <===== as expected | ||
const b = dropPrivateProps2({foo: 42, _bar: 'secret'}); // type is {foo: number, _bar: string} | ||
>b : Symbol(b, Decl(test.ts, 3, 5)) | ||
>dropPrivateProps2 : Symbol(dropPrivateProps2, Decl(test.ts, 0, 26)) | ||
>foo : Symbol(foo, Decl(test.ts, 3, 29)) | ||
>_bar : Symbol(_bar, Decl(test.ts, 3, 37)) | ||
|
||
//b._bar // no error, type of b._bar is string <===== NOT expected | ||
|
||
=== tests/cases/compiler/api.ts === | ||
import {excludePrivateKeys1, excludePrivateKeys2} from './internal'; | ||
>excludePrivateKeys1 : Symbol(excludePrivateKeys1, Decl(api.ts, 0, 8)) | ||
>excludePrivateKeys2 : Symbol(excludePrivateKeys2, Decl(api.ts, 0, 28)) | ||
|
||
export const dropPrivateProps1 = <Obj>(obj: Obj) => excludePrivateKeys1(obj); | ||
>dropPrivateProps1 : Symbol(dropPrivateProps1, Decl(api.ts, 1, 12)) | ||
>Obj : Symbol(Obj, Decl(api.ts, 1, 34)) | ||
>obj : Symbol(obj, Decl(api.ts, 1, 39)) | ||
>Obj : Symbol(Obj, Decl(api.ts, 1, 34)) | ||
>excludePrivateKeys1 : Symbol(excludePrivateKeys1, Decl(api.ts, 0, 8)) | ||
>obj : Symbol(obj, Decl(api.ts, 1, 39)) | ||
|
||
export const dropPrivateProps2 = <Obj>(obj: Obj) => excludePrivateKeys2(obj); | ||
>dropPrivateProps2 : Symbol(dropPrivateProps2, Decl(api.ts, 2, 12)) | ||
>Obj : Symbol(Obj, Decl(api.ts, 2, 34)) | ||
>obj : Symbol(obj, Decl(api.ts, 2, 39)) | ||
>Obj : Symbol(Obj, Decl(api.ts, 2, 34)) | ||
>excludePrivateKeys2 : Symbol(excludePrivateKeys2, Decl(api.ts, 0, 28)) | ||
>obj : Symbol(obj, Decl(api.ts, 2, 39)) | ||
|
||
=== tests/cases/compiler/internal.ts === | ||
export declare function excludePrivateKeys1<Obj>(obj: Obj): {[K in PublicKeys1<keyof Obj>]: Obj[K]}; | ||
>excludePrivateKeys1 : Symbol(excludePrivateKeys1, Decl(internal.ts, 0, 0)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 0, 44)) | ||
>obj : Symbol(obj, Decl(internal.ts, 0, 49)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 0, 44)) | ||
>K : Symbol(K, Decl(internal.ts, 0, 62)) | ||
>PublicKeys1 : Symbol(PublicKeys1, Decl(internal.ts, 1, 100)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 0, 44)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 0, 44)) | ||
>K : Symbol(K, Decl(internal.ts, 0, 62)) | ||
|
||
export declare function excludePrivateKeys2<Obj>(obj: Obj): {[K in PublicKeys2<keyof Obj>]: Obj[K]}; | ||
>excludePrivateKeys2 : Symbol(excludePrivateKeys2, Decl(internal.ts, 0, 100)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 1, 44)) | ||
>obj : Symbol(obj, Decl(internal.ts, 1, 49)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 1, 44)) | ||
>K : Symbol(K, Decl(internal.ts, 1, 62)) | ||
>PublicKeys2 : Symbol(PublicKeys2, Decl(internal.ts, 2, 64)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 1, 44)) | ||
>Obj : Symbol(Obj, Decl(internal.ts, 1, 44)) | ||
>K : Symbol(K, Decl(internal.ts, 1, 62)) | ||
|
||
export type PublicKeys1<T> = T extends `_${string}` ? never : T; | ||
>PublicKeys1 : Symbol(PublicKeys1, Decl(internal.ts, 1, 100)) | ||
>T : Symbol(T, Decl(internal.ts, 2, 24)) | ||
>T : Symbol(T, Decl(internal.ts, 2, 24)) | ||
>T : Symbol(T, Decl(internal.ts, 2, 24)) | ||
|
||
type PublicKeys2<T> = T extends `_${string}` ? never : T; | ||
>PublicKeys2 : Symbol(PublicKeys2, Decl(internal.ts, 2, 64)) | ||
>T : Symbol(T, Decl(internal.ts, 3, 17)) | ||
>T : Symbol(T, Decl(internal.ts, 3, 17)) | ||
>T : Symbol(T, Decl(internal.ts, 3, 17)) | ||
|
64 changes: 64 additions & 0 deletions
64
tests/baselines/reference/declarationEmitInlinedDistributiveConditional.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
=== tests/cases/compiler/test.ts === | ||
import {dropPrivateProps1, dropPrivateProps2} from './api'; | ||
>dropPrivateProps1 : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>dropPrivateProps2 : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
|
||
const a = dropPrivateProps1({foo: 42, _bar: 'secret'}); // type is {foo: number} | ||
>a : { foo: number; } | ||
>dropPrivateProps1({foo: 42, _bar: 'secret'}) : { foo: number; } | ||
>dropPrivateProps1 : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>{foo: 42, _bar: 'secret'} : { foo: number; _bar: string; } | ||
>foo : number | ||
>42 : 42 | ||
>_bar : string | ||
>'secret' : "secret" | ||
|
||
//a._bar // error: _bar does not exist <===== as expected | ||
const b = dropPrivateProps2({foo: 42, _bar: 'secret'}); // type is {foo: number, _bar: string} | ||
>b : { foo: number; } | ||
>dropPrivateProps2({foo: 42, _bar: 'secret'}) : { foo: number; } | ||
>dropPrivateProps2 : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
>{foo: 42, _bar: 'secret'} : { foo: number; _bar: string; } | ||
>foo : number | ||
>42 : 42 | ||
>_bar : string | ||
>'secret' : "secret" | ||
|
||
//b._bar // no error, type of b._bar is string <===== NOT expected | ||
|
||
=== tests/cases/compiler/api.ts === | ||
import {excludePrivateKeys1, excludePrivateKeys2} from './internal'; | ||
>excludePrivateKeys1 : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>excludePrivateKeys2 : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
|
||
export const dropPrivateProps1 = <Obj>(obj: Obj) => excludePrivateKeys1(obj); | ||
>dropPrivateProps1 : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
><Obj>(obj: Obj) => excludePrivateKeys1(obj) : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>obj : Obj | ||
>excludePrivateKeys1(obj) : { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>excludePrivateKeys1 : <Obj>(obj: Obj) => { [K in import("tests/cases/compiler/internal").PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>obj : Obj | ||
|
||
export const dropPrivateProps2 = <Obj>(obj: Obj) => excludePrivateKeys2(obj); | ||
>dropPrivateProps2 : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
><Obj>(obj: Obj) => excludePrivateKeys2(obj) : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
>obj : Obj | ||
>excludePrivateKeys2(obj) : { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
>excludePrivateKeys2 : <Obj>(obj: Obj) => { [K in keyof Obj extends `_${string}` ? never : keyof Obj]: Obj[K]; } | ||
>obj : Obj | ||
|
||
=== tests/cases/compiler/internal.ts === | ||
export declare function excludePrivateKeys1<Obj>(obj: Obj): {[K in PublicKeys1<keyof Obj>]: Obj[K]}; | ||
>excludePrivateKeys1 : <Obj>(obj: Obj) => { [K in PublicKeys1<keyof Obj>]: Obj[K]; } | ||
>obj : Obj | ||
|
||
export declare function excludePrivateKeys2<Obj>(obj: Obj): {[K in PublicKeys2<keyof Obj>]: Obj[K]}; | ||
>excludePrivateKeys2 : <Obj>(obj: Obj) => { [K in PublicKeys2<keyof Obj>]: Obj[K]; } | ||
>obj : Obj | ||
|
||
export type PublicKeys1<T> = T extends `_${string}` ? never : T; | ||
>PublicKeys1 : PublicKeys1<T> | ||
|
||
type PublicKeys2<T> = T extends `_${string}` ? never : T; | ||
>PublicKeys2 : PublicKeys2<T> | ||
|
18 changes: 18 additions & 0 deletions
18
tests/cases/compiler/declarationEmitInlinedDistributiveConditional.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @declaration: true | ||
// @filename: test.ts | ||
import {dropPrivateProps1, dropPrivateProps2} from './api'; | ||
const a = dropPrivateProps1({foo: 42, _bar: 'secret'}); // type is {foo: number} | ||
//a._bar // error: _bar does not exist <===== as expected | ||
const b = dropPrivateProps2({foo: 42, _bar: 'secret'}); // type is {foo: number, _bar: string} | ||
//b._bar // no error, type of b._bar is string <===== NOT expected | ||
|
||
// @filename: api.ts | ||
import {excludePrivateKeys1, excludePrivateKeys2} from './internal'; | ||
export const dropPrivateProps1 = <Obj>(obj: Obj) => excludePrivateKeys1(obj); | ||
export const dropPrivateProps2 = <Obj>(obj: Obj) => excludePrivateKeys2(obj); | ||
|
||
// @filename: internal.ts | ||
export declare function excludePrivateKeys1<Obj>(obj: Obj): {[K in PublicKeys1<keyof Obj>]: Obj[K]}; | ||
export declare function excludePrivateKeys2<Obj>(obj: Obj): {[K in PublicKeys2<keyof Obj>]: Obj[K]}; | ||
export type PublicKeys1<T> = T extends `_${string}` ? never : T; | ||
type PublicKeys2<T> = T extends `_${string}` ? never : T; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T extends `_${string}`
bit will never actually have anything to distribute over because the distribution is done one level up.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other thing I was curious about is whether we could ever just copy the unreachable type alias declaration to a local one. If we’re already copying its contents, why not just drop the whole declaration nearby with a unique name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wherever we can, we do (specifically if we can preserve it as-is, in it's existing location). We only do inlining like this when the type comes from somewhere unreachable or elided (like inside a different module's locals or an elided function body). We don't copy from those scopes into the current scope because then we'd have to do 1. pull in its whole dependency tree of types (which may, in turn, depend on things we just can't copy, like signature type parameters), and 2. ensure everything is renamed in such a way where there's no name conflicts. It's just not something we do much of right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I thought at first, but don’t we end up having to do that if we inline it too? Like the only part of the type that we don’t copy is the declaration.