TS2551: Property XXX does not exist on type
🔎 Search Terms
"property does not exist", "property does not exist under if statement", "property not allowed after if statement excluding it"
🕗 Version & Regression Information
- This changed between versions 5.0.0-beta and 5.9.0-dev.20250318
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about my problem
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250318#code/MYewdgzgLgBAsgeQCIFUAyBRA+gYQQOQDEBJAcRgF4YBvAKBhlDCgFNmAuG+hx8V5rBGAAnAJYAHKBE50ePALYBDKMAAWLaTADaAclVQo49gHpjAKnM6ANDD0Hx00xbPXbAHkUAbT1gCuwzwgAPh0AXStuOQArTS0AAyZ+KGNRMAATFgAPADpRUQAzFmyYuPDImABfCIYq2gqYRQheSCgAblpaJmgYJAxCAEF0ABVcAAkEYhwMAGVKbW5qGDBFeRZOHRw+NlhpkQlYAAoMTJZgX1YYAClZ8BgAdRYAIxgABUUAcxYASlcANy9fGtbIltjpKtUaEsVkCNltmDBdmJJDAAEq+ZiiVYwA7EMBRU6wa4wW7TcSnAqiYAwfrAKCicBNTyiADWLFeIHEvnEMBwTOAzJ+Nn+nkB6xBzAAtMJ0XTVmDaqEGk0um0OllxCBhLAoABPMnwEBpXyeFj4aFDPVsqgHXVkkD5Hp9QZoEY4caTGZfLRgXzyR4sYShXTCwFhdq0dWa2AqmBZFbiE1zRo6sBUg7ceSG42m6GcOBZk1m1YWskRL6UIJcBgFbGZo2F6HZaCKLUQO6iKCqA46cVQHRfcuyHg1g517NFq0UKg9uFQKUyzEsftVuQwUwwUYYFEYHSzIabmAYfBIGAIQgwfcYGBoYgANSvZ+Bs-nGLlNgAQighhfZkgEDN8B0IZtzwUh8GIAAtK9iG-Yh8EPNBpivaYhn6IYMDgI8hnKBhhBYKB-DAdo5Aqbh6hYQI2SHBh10-b9L23XdqXgrcUQQFFyi6EATWyTwQHeA5EFQTBcAIEhSC0McG1WUIvg6OR12IGB8AwDATyGBBXjQfophgGDygKUcCxzLEAEIpyfZhthfWUly+ajVy4ni+IEoT0GwPAiDISTjInWTylIhTjFPDigpgAABKQJXVAlouEYRNRwngnKKFzBOQdzRK8iSpJMlh-JqeTmggbjUv4g44iQCj8JYNJOAAEmoXKJwqOIvnaCpwzjeQExYbtexsxd+1aIA
💻 Code
Code is very, very simple, let's look on playground
🙁 Actual behavior
TS2551: Property content-runtime does not exist on type
🙂 Expected behavior
Not occur, because of part TS complaining about, not exists on else statement at all.
startsWith doesn't participate in narrowing out of the box. If you want this, you can add it yourself:
declare global {
interface String {
startsWith<K extends string>(searchString: K): this is `${K}${string}`
}
}
This will fix your example.
startsWithdoesn't participate in narrowing out of the box. If you want this, you can add it yourself:declare global { interface String { startsWith<K extends string>(searchString: K): this is
${K}${string}} } This will fix your example.
Thanks, but it looks like overkill, for my problem, i mean that should work with native solution.
Why it doesn't work and have you plan to implement solution for this?
Ahh, I found the original. Duplicate of https://github.com/microsoft/TypeScript/issues/46958; search terms startsWith in:title.
have you plan to implement solution for this?
See https://github.com/microsoft/TypeScript/issues/46958#issuecomment-983054296