Repository: TypeScript
Issue: Literal / Known keyof T (especially for mapped types)
Labels: Suggestion

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.

Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ, especially the "Common Feature Requests" section: https://github.com/Microsoft/TypeScript/wiki/FAQ

-->

## Search Terms

<!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->
literal known keyof union primitive subtract subtraction generic mapped exact

## Suggestion

<!-- A summary of what you'd like to see added or changed -->

`keyof T` has issues with mapped types when T is generic but still has known keys.
The resulting mapped type has indeterminate keys despite having some of the keys known. 

It would be helpful to be able to map the known keys only in order to have a determinate result type.

Propose adding something like a `known keyof` operator that would resolve to only the known/literal keys of an object.

## Use Cases

<!--
What do you want to use this for?
What shortcomings exist with current approaches?
-->

Would like to use this to validate property / object compatibility, especially with the upcoming recursive conditional types. 
A major use case is supporting property path tuples (or dotted paths, as shown in the example in https://github.com/microsoft/TypeScript/pull/40336)
Having `known keyof` would allow validation of a path tuple parameter recursively without having to generate all the possible valid values for a type (quickly running into the union cap).

```ts
type _DottedPath<T, K extends string & keyof T = string & keyof T> = K extends any ? K | `${K}.${_DottedPath<NonNullable<T>[K]>}` : never;
type DottedPath<T> = _DottedPath<T>;

//This one is unworkable
type PathGetterExpensive<T> = <TPath extends DottedPath<T>>(obj: T, path: TPath) => PropType<T, TPath>;
//This should work
type PathGetter<T> = <TPath extends string>(obj: T, path: OnlyIfPathCompatible<T, TPath>) => PropType<T, TPath>;

//Playground link below has an example of this using `keyof` instead of `known keyof`, and the issue it causes
type OnlyIfPathCompatible<T, TPath extends string, TOut = TPath> = 
    TPath extends known keyof T ? TOut :
    TPath extends `${infer K}.${infer R}` ? (K extends known keyof T ? OnlyIfPathCompatible<T[K], R, TOut> : never) :
    never;
```

Another use case is catching issues where a type T has been inferred to `any` unexpectedly and I would prefer a parameter of type `keyof T` to be `never` rather than `keyof any`.

## Examples

<!-- Show how this would be used and what the behavior would be -->
```ts
type Obj = { a: number; b: number; c: string; };

type KeysByType<O, T> = { [k in keyof O]-?: O[k] extends T ? k : never; }[keyof O];

type KeyofExtends<T extends Obj> = keyof T; //indeterminate - which can be a problem.
type NumericProps<T extends Obj> = KeysByType<T, number>; //indeterminate - which can be a problem.

function contrivedIssue<T extends Obj>(obj: T) {
	const k: keyof T = "a";
	const numericK: KeysByType<T, number> = "a"; //Error, though it shouldn't be.
	const va: number = obj.a; //Because TS already knows that T has property `a` of type `number`.
	return [numericK, va];
}

/*
// Proposed: `known keyof`
type KnownKeyofExtends<T extends Obj> = known keyof T; // "a" | "b" | "c";
type KnownKeyofAny = known keyof any; // never;

type KnownKeysByType<O, T> = { [k in known keyof O]-?: O[k] extends T ? k : never; }[known keyof O];

type KnownNumericProps<T extends Obj> = KnownKeysByType<T, number>; // "a" | "b";
*/

//Another real world use case is catching when inferred to any.

function multiplyPropsG<T>(obj: T, propA: KeysByType<T, number>, propB: KeysByType<T, number>) {
	//@ts-expect-error
	return obj[propA] * obj[propB];
}

//Good
const axb = multiplyPropsG({ a: 1, b: 2 } as const, "a", "b");
//@ts-expect-error - Good
const axbInvalid = multiplyPropsG({ c: 1, d: 2 } as const, "a", "b");
//@ts-expect-error - Preferably
const axbAny = multiplyPropsG({ c: 1, d: 2 } as any, "a", "b");

/*
function multiplyKnownPropsG<T>(obj: T, propA: KnownKeysByType<T, number>, propB: KnownKeysByType<T, number>) {
	return obj[propA] * obj[propB];
}

//This would be an error
const axbKnownAny = multiplyKnownPropsG({ c: 1, d: 2 } as any, "a", "b");
*/
```
[Playground](https://www.typescriptlang.org/play?ts=4.1.0-insiders.20200916&t=true#code/C4TwDgpgBA8gRgKygXigbygQwFxQHYCuAtnBAE4DcUcuhJ5VAxrgM7BkCWeA5lQL4UAUINCQoAaQggWAIRAAVcBAA8MADRR5APhT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GYKBPu+ua+AW8yQEB9lgdBjt2ysmE4VGR68ec+i8GGzzvLJTQopTQKAIY4KmOuFiHRlQWwBikXZzFkCwql9jkqKxoSpqhA6qFqqtZ12WGqE+Q+kCLpNXkwERg8AqyDRrqeq5rG+tx-GueGt6SbJ3AKapj9afp-jBKa-cABFRJ8YytjaHozxuu62lZv9Ugo+LJej9VkOONhOB4UpBCgQv9WRHgs4JDPBhCKENxHMIC6Liuy8COC0M0fQK-cbB68LxvqOODMABI0C4RGJD4RYh5H8SACU+BzEJxCbt8KhsVvU+Y-RxFsDRp50XBq9fLui4UvAa7wQiU5jtzGiaTzdFyXuV0CHOtK80lfMmGOXDwZy0SsCuADCFgiBgETqCSU8gK5aCpMgHQ68JYaEgTHR4ycxAwB-iAP+gDgGgNhOAyWSDM59wJC-HgiCYAED1KgQhPQzjd00I-Rcy9WTBE0BQvUR8i40KXoPYeeBR7iHHpPfhM856sKJIvYhzdtyt3Qb-f+McgEgLAfMXIm9t5QGnuQyhe9vxYSpJwwueAfyUFQdAAAGroMauAMDTUSkwVgJdeAxH4EIBojBZiYGDP9dGnhcDmLcZ6Tx3jOp6m4I0CiAA1KS2JcAV2vsAW+5iUHhOAFEmJEAiSeFpqDVJ6TZjYiyYDRY61NrVHBjtPaB1Cp5N2NEgpmTslcVKcIZmNlRjhOMbWDEWIcRL2SXzD+qSmhxKvjfcgnk1Y0mGeQPmOSvyDjwivHMpgMxyMwQo9USjcEcFBDmA0FtjLzD1OqDggR45+E+LSGZZA5lcRKSDBZZ9mHjmgpmdZWDFE4JURAfZhp5LHJMqbKAFyrk0i2hDXaUNql22meM25pJAalM4j8eYQKzn6n+cZNgHBZjSStqMGGZBljNiAA)

## Checklist

My suggestion meets these guidelines:

* [X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [X ] This wouldn't change the runtime behavior of existing JavaScript code
* [X ] This could be implemented without emitting different JS based on the types of the expressions
* [X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [X ] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).