-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Force extra property checks if object is explicitly casted or at-least give us a StrictPropertyCheck/Exact type #32537
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
Comments
Sounds like #7481, though it's hard to tell because the example doesn't require a type assertion. |
I'm not sure I understand #7481 As per the typescript play example I would like someway of telling TS to give me stricter property checks that flow through form interface to concrete implementation. i.e interface IComponent<State> {
state: State;
config: {
defaultState?: State; // some way to say I want this strict e.g StrictPropertyCheck<State>
template?: (state: State) => any;
}
update: (stateUpdate: Partial<State>) => void;
}
class Component<State> implements IComponent<State> {
public state: State;
constructor() {
this.state = Object.assign({} as State, this.config.defaultState);
}
public get config() {
return {
defaultState: {} as State,
}
}
update(stateUpdate: Partial<State>) {
Object.assign(this.state, stateUpdate);
}
}
interface SwitchState {
on: boolean;
}
class SwitchComponent extends Component<SwitchState> {
public get config() {
return {
defaultState: {
on: false,
blah: 123, // no error, ¯\_(ツ)_/¯ - dead code - I want TS to throw error for extra properties
},
}
}
} |
There's already a way to do that, though - put a return type annotation on #7481 covers the pros and cons of various options here |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.1.1
Search Terms: extra property checks, explicit cast, exact type
Code
Expected behavior:
Explicit cast should force extra property checks like the object literal does. I feel like TS team thinks this is intentional and may be a big breaking change.
Alternatively please give us an exact type if possible e.g
StrictPropertyCheck<T>
so we can do something likeconst b = <StrictPropertyCheck<SwitchState>>{...}
This stackoverflow thread has some magic vodoo for forcing extra property checks for functions that take arguments. https://stackoverflow.com/questions/54775790/forcing-excess-property-checking-on-variable-passed-to-typescript-function
Would be nice if we could have a similar mapped type that worked for objects.
Actual behavior:
No error
Playground Link:
https://typescript-play.js.org/#code/JYOwLgpgTgZghgYwgAgJIGED2BbADpkCcAHgGUw5IA+ZAbwChknmBnCyALmXMogG5GzJggIxgAcy4MhM5ABMI8AK4AbMD0gB+Lhv7IA9PrTIA7nHDzFcVevYowmZACMUbKMARgVAT2QIAFhAIANYQcshw4nCgbNx2yLhQmLjQYMAQLIKyTJB4KrzayAAUbLw6dgCUyAC8NObeArIAvlnMSrhyZcWlkACqHV0ACnBQaXAqZHZUVbXIAG6YwHICLfQI+SwsyFh4BERgk7w0wHkQ2PtbGDj4hCS6NNLMuEpOKh7IPRDlvAKtIiBuJSeTBQIpVR6yMD+YAsAB0nxqyAA8k4AFZBMCwuCbCQgIq0JoRLa6AA0yChMNh-zE4lhCmUal0FUazFWQmer3e4ggYD8ogkYLorSEUB5SigICF2Rk9OsjLsUkJ2LivBJwtZrTZbQGkBKdn6nU4yGGo2A40O1HB6qYKPRnixOPEeIpcM+ZM+Bt4zM19FW9FAkFgiBQpBMwDAAV0UuYBC4TkwmBUEHMK3oaw2xLDEf8Oxu+2QEAAHpAQHItrm9ncs5GptGmBy3ghkNzedSBVbpaKwOLJRDpZYGbYumRq-57rQDEYAOqYVThFzIEAeFDAGDk6FbEwg0LhLP+We8ou4RvhvzY3kOTDW-ux5DwFQsCBq-vZV5wfxcACMACYAMxkwxF0caAkigMkAHqAB0AH0ikAEYYKmg-RwKYABaSw4HCEQFGvIQmmfbIWjwtNfTTf5YjgRER3DGsjloW970fMk3w-ZAf1-Jo+EnICCygUCIJg+DEOQ5A1gIWInB0Uco2qOgGPGJjnHyVj2M47iQJBMlsDgXwCB8D4UgQVdfGCEBMBMSVEmSaBfHMedlOQGEC2LKA4CAA
Related Issues:
#9312, #28464
The text was updated successfully, but these errors were encountered: