TypeScript/tests/cases/compiler/conditionalTypesSimplifyWhenTrivial.ts
Wesley Wigham a9ad94ab3c
Conditional type simplifications & Globally cached conditional type instances (#29437)
* Introduce simpliciations for extract/exclude-like conditional types and fix restrictive instantiations

* Add test for the common simplifications

* unify true branch constraint generation logic and true branch simplification

* Use identical check on instantiated types

* Add late-instantiate conditionals to test

* Globally cache conditional type instantiations ala indexed access types

* Handle `any` simplifications

* Factor empty intersection check into function

* Modifify conditional type constraints to better handle single-branch `any` and restrictive type parameters

* Add test case motivating prior commit

* Fix lint

* Factor logic into worker vs cacheing function

* Remove now unneeded casts
2019-03-08 15:33:12 -08:00

66 lines
1.7 KiB
TypeScript

// @strict: true
const fn1 = <Params>(
params: Pick<Params, Exclude<keyof Params, never>>,
): Params => params;
function fn2<T>(x: Exclude<T, never>) {
var y: T = x;
x = y;
}
const fn3 = <Params>(
params: Pick<Params, Extract<keyof Params, keyof Params>>,
): Params => params;
function fn4<T>(x: Extract<T, T>) {
var y: T = x;
x = y;
}
declare var x: Extract<number | string, any>; // Should be `numebr | string` and not `any`
type ExtractWithDefault<T, U, D = never> = T extends U ? T : D;
type ExcludeWithDefault<T, U, D = never> = T extends U ? D : T;
const fn5 = <Params>(
params: Pick<Params, ExcludeWithDefault<keyof Params, never>>,
): Params => params;
function fn6<T>(x: ExcludeWithDefault<T, never>) {
var y: T = x;
x = y;
}
const fn7 = <Params>(
params: Pick<Params, ExtractWithDefault<keyof Params, keyof Params>>,
): Params => params;
function fn8<T>(x: ExtractWithDefault<T, T>) {
var y: T = x;
x = y;
}
type TemplatedConditional<TCheck, TExtends, TTrue, TFalse> = TCheck extends TExtends ? TTrue : TFalse;
const fn9 = <Params>(
params: Pick<Params, TemplatedConditional<keyof Params, never, never, keyof Params>>,
): Params => params;
function fn10<T>(x: TemplatedConditional<T, never, never, T>) {
var y: T = x;
x = y;
}
const fn11 = <Params>(
params: Pick<Params, TemplatedConditional<keyof Params, keyof Params, keyof Params, never>>,
): Params => params;
function fn12<T>(x: TemplatedConditional<T, T, T, never>) {
var y: T = x;
x = y;
}
declare var z: any;
const zee = z!!!; // since x is `any`, `x extends null | undefined` should be both true and false - and thus yield `any`