question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filter object properties by type

See original GitHub issue

TypeScript 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
shaunscommented, Sep 29, 2020

In 4.1 (beta) you can do something like:

type PickByValueType<T, U> = {
  [K in keyof T as T[K] extends U ? K : never]: T[K]
}
3reactions
aspic-fishcommented, Feb 28, 2022

I have a workaround, just infer type before mapping it. So rewrite this:

type FooMap<T> = {
    [key in FilteredKeys<T, number>]: FooItem<T[key]> 
}

to this:

type FooMap<T> = T extends infer TT 
  ?  {
    [key in FilteredKeys<T, number>]: FooItem<T[key]>
  }
  : never

turolla case

ziofat case

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found