microsoft / microsoft/TypeScript

TypedPropertyDescriptorMap<T>

Open
#40,137 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

TypedPropertyDescriptor
TypedPropertyDescriptorMap
Typed Property Descriptor Map

Suggestion

Include TypedPropertyDescriptorMap<T>, the typed version of PropertyDescriptorMap. The following type should do the trick:

type TypedPropertyDescriptorMap<T> = {
  [P in keyof T]: TypedPropertyDescriptor<T[P]>
}

This would require a change to the Object.defineProperties type definition since it only accepts a PropertyDescriptorMap type, an indexed type. Passing in an object with a type of TypedPropertyDescriptorMap<T> results in the error: Index signature is missing in type 'TypedPropertyDescriptorMap<T>'. There's a related issue on this topic (https://github.com/Microsoft/TypeScript/issues/15300).

Use Cases

It's a headache to have to define a type like the following when I already have defined the final object type:

// Final object looks like
interface MyProps {
  prop1: string
  prop2: (params: Record<string, string>) => Promise<void>
}

// My custom TypedPropertyDescriptor map
interface MyPropDescriptors {
  prop1: TypedPropertyDescriptor<MyProps['prop1']>
  prop2: TypedPropertyDescriptor<MyProps['prop2']>
  ...
}

Examples

// Final object looks like
interface MyProps {
  prop1: string
  prop2: (params: Record<string, string>) => Promise<void>
}

// My custom TypedPropertyDescriptor map
type MyPropDescriptors = TypedPropertyDescriptorMap<MyProps>

Checklist

My suggestion meets these guidelines:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the declaration for Object.defineProperties and the existing TypedPropertyDescriptor and PropertyDescriptorMap types. Review related issue #15300, then verify the proposed mapped type and its acceptance by Object.defineProperties with a focused type-checking test; done means typed descriptor maps work without the index-signature error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.