1

I am getting the error:

Element implicitly has an 'any' type because expression of type '"bla"' can't be used to index type 'Record<string, any> | PropertiesI'.
  Property 'bla' does not exist on type 'Record<string, any> | PropertiesI'.ts(7053)

In the following typescript:

interface PropertiesI {
  bar: string;
}

interface ParentSchemaI {
  foo: string;
  properties: PropertiesI | Record<string, any>;
}

const mySchema: ParentSchemaI = {
  foo: "baz",
  properties: {
    bla: "blabla", // This doesn't give me an error
  },
};

const attempt = mySchema.properties["bla"]; // yet this flags as an error?

Why would typescript allow me to create a property on a new object and then throw an error when I try to access the same property?

JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71
  • In the case that `properties` is `PropertiesI`, then it would be invalid. – kelsny Oct 13 '22 at 12:55
  • I understand, but I am trying to get it to accept either `PropertiesI | Record`. How do I get it to accept a Record as well? – JimmyTheCode Oct 13 '22 at 14:21
  • That simplifies to only `Record` then... – kelsny Oct 13 '22 at 14:40
  • I've provided a simplified example. In my use case I want PropertiesI to be included for intellsense, etc. – JimmyTheCode Oct 13 '22 at 14:46
  • I think you don't want to annotate `mySchema` as `ParentSchemaI`, you just want it to *satisfy* that type instead of being *widened* to that type. TS4.9 will introduce [a new `satisfies` operator](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9-beta/#hamilton) that behaves this way; it looks like [this code](https://tsplay.dev/w6v10w) for your example. Does that address your question fully? If so I could write up an answer; if not, what am I missing? (Please mention @jcalz to notify me) – jcalz Oct 13 '22 at 16:03
  • Ah great find! That's certainly some way to what I want. I've added to your example [here](https://www.typescriptlang.org/play?ts=4.9.0-dev.20221013#code/JYOwLgpgTgZghgYwgAgApQPYAdpmBAZwElkBvAWAChlkAjOKALmQLClAHMBuKgXyqqhIsRClQMI4AMoIAFhAC2cEhWrIYGDM1bsQ3KjSyYcUPIWbpsufMWQAfZACUICDFAAmAHh2cANMjgQAE8APh5KfkoqVxBWZAUgmXklZABeMgN1TWYAInoALxzfTKMrUxtmVRoaWgAbOFy6uCaczN5iiJY4PAIYGzQJaTlFZXDojFiwALBIBSwp9ISkkYA6UpMzAgBtPPqcgF0uZAFKGLil4aUAJjSMtQ0tZByfPSKS42tzKur6JmQr-wAekByCksgwAFdau5kAg4BCCChoJgoCxQEhkCR3BA+iAIDDgFM4AQWGxOMgJshaqAUFdMjQCOCoe4AEIQACCwWYAGYACzHNSRPhdHp9QgDKCSMDLJREIA). Just one prob. – JimmyTheCode Oct 14 '22 at 13:49
  • Adding to my previous comment @jcalz , I think my problem is that I'm trying to do something that typescript doesn't allow. It's described [here](https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i) I believe. You can't hard-type some prop/values and then leave others optional. – JimmyTheCode Oct 14 '22 at 13:54
  • Note that I was not notified of your previous comment, because it didn't have @jcalz in it. Luckily your second comment got my attention. Yeah, that's an issue with TS where there's no "rest" index signature that says "everything *else* has to be a particular type" and the other answer you linked is my best effort on various workarounds. So, do you want me to write up an answer here about `satisfies`? Or should I leave it alone? (And remember, @jcalz or I won't be notified) – jcalz Oct 14 '22 at 14:00
  • Yeah, please do. I think if you write your answer and add that about the lack of spread operator, etc then that's about as full an answer as anyone could hope for :-) – JimmyTheCode Oct 14 '22 at 14:05
  • Sorry, with tag! @jcalz – JimmyTheCode Oct 17 '22 at 07:54

0 Answers0