Filter object properties by type
See original GitHub issueTypeScript Version: 3.9.2
Search Terms: mapped type filter object properties never
Expected behavior:
The type
type FilteredKeys<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];
that allows to extract object properties by type should work everywhere.
Actual behavior:
type FilteredKeys<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];
only works externally of the declaring type
Related Issues: #23199
Code
type FilteredKeys<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T];
type FooItem<T> = T extends number ? { value: T } : undefined;
type FooMap<T> = {
// [key in keyof T]: FooItem<T[key]> // <-- This works
[key in FilteredKeys<T, number>]: FooItem<T[key]> // <-- This doesn't work
}
class Bar {
a = 1;
b = 2;
c = '';
doSomething() {
const fooMap: FooMap<this> = undefined!; // Omitted...
const a = fooMap.a.value; // <-- No error expected
const c = fooMap.c.value; // <-- Error expected
}
}
const fooMap2: FooMap<Bar> = undefined!; // Omitted...
const a = fooMap2.a.value; // <-- No error expected
const c = fooMap2.c.value; // <-- Error expected
Output
"use strict";
class Bar {
constructor() {
this.a = 1;
this.b = 2;
this.c = '';
}
doSomething() {
const fooMap = undefined; // Omitted...
const a = fooMap.a.value; // <-- No error expected
const c = fooMap.c.value; // <-- Error expected
}
}
const fooMap2 = undefined; // Omitted...
const a = fooMap2.a.value; // <-- No error expected
const c = fooMap2.c.value; // <-- Error expected
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": false,
"alwaysStrict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"downlevelIteration": false,
"noEmitHelpers": false,
"noLib": false,
"noStrictGenericChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"preserveConstEnums": false,
"removeComments": false,
"skipLibCheck": false,
"checkJs": false,
"allowJs": false,
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"target": "ES2017",
"module": "ESNext"
}
}
Playground Link: Provided
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Typescript: How do you filter a type's properties to those of a ...
In this, KeysMatching<T, V> returns the set of keys of T whose properties are assignable to V . It uses a conditional and...
Read more >Filtering an Object in TypeScript - Steve Ruiz
Ever need to filter properties from an object? Here's how to do it. In JavaScript: function filterObject(obj, fn) { return Object.
Read more >TypeScript Essentials: Conditionally Filter Types | by Florian
Learn how to conditionally filter types in TypeScript and demystify ... Construct a new object-based type by picking the properties we want ...
Read more >JavaScript filter object type tutorial - Nathan Sebhastian
When you want to filter an object by its property value, you need to use the same methods you just need to change...
Read more >Filter attributes by types in Typescript | VLK Studio - Medium
The type for each property will be the name of the key, if the value of ... With this type I can easily...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In 4.1 (beta) you can do something like:
I have a workaround, just infer type before mapping it. So rewrite this:
to this:
turolla case
ziofat case