1

I've gotten this far: which seems to work

function test<types extends Record<string,any>>(dict: dictionary<types>){}

type dictionary<types extends Record<string, any>> = {
  [key in keyof types]: {
    bar?: types[key];
    foo?: (value:types[key])=>true;
  };
};

test({
 key1:{
  bar: 2,
  foo: (input:number)=>true,
 },
 key2:{
  bar: 'hello'
  foo: (input: number)=>true, // Error! "input" needs to be string
 }
})


BUT! I also need a generic type reference to the dict parameter. And for some reason, this doesn't work


function test2<
  types extends Record<string,any>,
  dictionary extends dictionary2<types> // <-- Added a generic type
>(dict: dictionary){}

// Same as above
type dictionary2<types extends Record<string, any>> = {
  [key in keyof types]: {
    bar?: types[key];
    foo?: (value:types[key])=>true;
  };
};

// Same as above
test2({
 key1:{
  bar: 2,
  foo: (input: number)=>true,
 },
 key2:{
  bar: 'hello', 
  foo: (input:number)=>true,// Should be an Error (but isn't)! "input" needs to be string
 }

Playground link

2
  • Please consider this example. This is related question. There are drawbacks. You need to use methods because they are bivariant. Let mew know if it works. If you know upfront your values, you can use this example. However, the best approach is to use builder function. It is easy, readable and safe Commented Dec 5, 2021 at 20:00
  • Builder example-> here) Commented Dec 5, 2021 at 20:06

2 Answers 2

1

You could do like this:

function test2<T extends Record<string, unknown>>(dict: Dictionary<T>) { }

type Dictionary<T> = {
  [key in keyof T]: {
    bar?: T[key];
    foo?: (value: T[key]) => true;
  };
}

// Same as above
test2({
  key1: {
    bar: 2,
    foo: (input: number) => true,
  },
  key2: {
    bar: 'hello',
    foo: (input: number) => true, // Actual error
  }
});

TypeScript playground

Sign up to request clarification or add additional context in comments.

1 Comment

Seems to be there was a much simplier solution than I thought. Nice!
0

Change the inference scope so that types is inferred and dict is typed based on that inference rather than a second type parameter, i.e.,

function test2<
  types extends Record<string,any>
>(dict: dictionary2<types>){}

Working playground here.

EDIT: Example usage with conventional capitalization

function test2<
  T,
>(dict: Dictionary<T>){

  type FullDictionary = Dictionary<T> // can't you just use this in your function?

}

type Dictionary<T extends Record<string, any>> = {
  [K in keyof T]: DictionaryEntry<T[K]>
}

type DictionaryEntry<T> = {
  bar?: T
  foo?: (value:T)=>true
}

test2({
 key1:{
  bar: 2,
  foo: (input: number)=>true,
 },
 key2:{
  bar: 'hello', 
  foo: (input:number)=>true
 }
})

4 Comments

Correct me if I'm wrong, but this is the same as the code I currently have isn't it (i.e. given at the top of my question)
oh, ha. but then i guess i'm confused at what your question is. why can't you just use dictionary<types>? btw, its common to write this code with capitalized types for readability, e.g.: function test<T extends Record<string,any> and type Dictionary = etc.
I need a generic reference to the dictionary type to check if certain properties exist
but anywhere you want to use that generic, can't you just use dictionary<types>? i've edited my answer to show what i mean. in my edited answer, isn't FullDictionary giving you what you want? what what a generic type parameter allow you to do that you can't do with that? (sorry if i'm being dense)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.