-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
satisfies
operator fails to recognize optional properties that are defined
#54836
Comments
As far as I know, // simulate #42384
function addProp<T extends object, K extends PropertyKey, V>(
obj: T, k: K, v: V): asserts obj is T & Record<K, V> {
Object.assign(obj, { [k]: v });
}
function createUser(id: string): User {
const newUser: UnfinishedUser = { id };
addProp(newUser, "someAction", () => { /* ... */ });
return newUser satisfies User; // okay
}
function createUser2(id: string): User {
const newUser: UnfinishedUser = { id };
addProp(newUser, "someAction", () => { /* ... */ });
return newUser; // still okay
} |
I understand, I think what I'm looking for is more explicit type guards. Using type User = {
readonly id: string,
readonly someAction: () => void
}
type UnfinishedUser = {
id: string,
someAction?: () => void
}
function createUser(id: string): User {
const newUser: UnfinishedUser = { id };
newUser.someAction = () => { /* ... */ };
return newUser satisfies User as User; // fails
} My goal is to ensure |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
"partial" "satisfies" "narrow"
π Version & Regression Information
Tried on version 5.1.5
β― Playground Link
https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true#code/C4TwDgpgBAqgzhATlAvFA3gWAFBT1RCAQwBMB7AOwBsQoBLEgLijmETooHMAaHfA4uWq04ZALYQAggGNgdSswAUASlQA+KADcyDHAF8cOUJCgBZAK7AiAIyoQAPABUNaLLnwBaQqUo0oAbQBpegooAGsIEDIAMyhHAF1mRyD4-UNsaPMKWXlQ6W9gCHgkRQZmVnYuZWZi5Dd+aUpWKAoIAHda5gsrWwcABSJEOSIqe1q1Fwx6Eig9AG50-laOhEQAOlEJGTlKVCgVdSmAegAqKDWLqBOj2YXsPnxCYHNEUOXaliI5OGi6CDhYKs5ngjjdokQ6FQ4GlsEA
π» Code
π Actual behavior
newUser
does not satisfyUser
π Expected behavior
newUser
should satisfyUser
The text was updated successfully, but these errors were encountered: