Skip to content

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
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
38 changes: 37 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5102,13 +5102,49 @@ namespace ts {

function conditionalTypeToTypeNode(type: ConditionalType) {
const checkTypeNode = typeToTypeNodeHelper(type.checkType, context);
context.approximateLength += 15;
if (context.flags & NodeBuilderFlags.GenerateNamesForShadowedTypeParams && type.root.isDistributive && !(type.checkType.flags & TypeFlags.TypeParameter)) {
const newParam = createTypeParameter(createSymbol(SymbolFlags.TypeParameter, "T" as __String));
const name = typeParameterToName(newParam, context);
const newTypeVariable = factory.createTypeReferenceNode(name);
context.approximateLength += 37; // 15 each for two added conditionals, 7 for an added infer type
const newMapper = prependTypeMapping(type.root.checkType, newParam, type.combinedMapper || type.mapper);
const saveInferTypeParameters = context.inferTypeParameters;
context.inferTypeParameters = type.root.inferTypeParameters;
const extendsTypeNode = typeToTypeNodeHelper(instantiateType(type.root.extendsType, newMapper), context);
context.inferTypeParameters = saveInferTypeParameters;
const trueTypeNode = typeToTypeNodeOrCircularityElision(instantiateType(getTypeFromTypeNode(type.root.node.trueType), newMapper));
const falseTypeNode = typeToTypeNodeOrCircularityElision(instantiateType(getTypeFromTypeNode(type.root.node.falseType), newMapper));


// outermost conditional makes `T` a type parameter, allowing the inner conditionals to be distributive
// second conditional makes `T` have `T & checkType` substitution, so it is correctly usable as the checkType
// inner conditional runs the check the user provided on the check type (distributively) and returns the result
// checkType extends infer T ? T extends checkType ? T extends extendsType<T> ? trueType<T> : falseType<T> : never : never;
// this is potentially simplifiable to
// checkType extends infer T ? T extends checkType & extendsType<T> ? trueType<T> : falseType<T> : never;
// but that may confuse users who read the output more.
// On the other hand,
// checkType extends infer T extends checkType ? T extends extendsType<T> ? trueType<T> : falseType<T> : never;
// may also work with `infer ... extends ...` in, but would produce declarations only compatible with the latest TS.
return factory.createConditionalTypeNode(
checkTypeNode,
factory.createInferTypeNode(factory.createTypeParameterDeclaration(/*modifiers*/ undefined, factory.cloneNode(newTypeVariable.typeName) as Identifier)),
factory.createConditionalTypeNode(
factory.createTypeReferenceNode(factory.cloneNode(name)),
typeToTypeNodeHelper(type.checkType, context),
factory.createConditionalTypeNode(newTypeVariable, extendsTypeNode, trueTypeNode, falseTypeNode),
factory.createKeywordTypeNode(SyntaxKind.NeverKeyword)
),
factory.createKeywordTypeNode(SyntaxKind.NeverKeyword)
);
}
const saveInferTypeParameters = context.inferTypeParameters;
context.inferTypeParameters = type.root.inferTypeParameters;
const extendsTypeNode = typeToTypeNodeHelper(type.extendsType, context);
context.inferTypeParameters = saveInferTypeParameters;
const trueTypeNode = typeToTypeNodeOrCircularityElision(getTrueTypeFromConditionalType(type));
const falseTypeNode = typeToTypeNodeOrCircularityElision(getFalseTypeFromConditionalType(type));
context.approximateLength += 15;
return factory.createConditionalTypeNode(checkTypeNode, extendsTypeNode, trueTypeNode, falseTypeNode);
}

Expand Down
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]; };
Copy link
Member

Choose a reason for hiding this comment

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

  1. Is it not a problem that the middle conditional type (the inner of the two synthesized ones) is distributive itself? It seems like it comes out the same, but in a way the semantics are slightly different here, right?—the T extends `_${string}` bit will never actually have anything to distribute over because the distribution is done one level up.
  2. Whether or not (1) actually matters, you could use Ron’s new infer constraints here instead, which would reduce a level of nesting. That seems preferable to me, unless we’re specifically trying to avoid emitting that for back compat reasons, but... I think that’s a job for downlevel-dts.

Copy link
Member Author

@weswigham weswigham Apr 6, 2022

Choose a reason for hiding this comment

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

  1. Extra layers of distributivity don't matter, so long as the right type variable is distributed over before the conditional logic occurs.
  2. Yeah, I mention that in a comment in the code - we could, but we'd start producing declaration files incompatible with older versions of TS and I don't know if we wanna do that just yet (considering they don't come from any input code positions). It's always something we could change down the line.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

pull in its whole dependency tree of types

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.

//// [test.d.ts]
export {};
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))

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>

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;