You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Force extra property checks if object is explicitly casted or at-least give us a StrictPropertyCheck/Exact type #32537
Search Terms: extra property checks, explicit cast, exact type
Code
interfaceSwitch{on: boolean;}consta=<Switch>{on: false,blah: 123};// no error, ¯\_(ツ)_/¯ constb: Switch={on: false,blah: 123};// error, may only specify known propery and blah is extra
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 like const b = <StrictPropertyCheck<SwitchState>>{...}
changed the title [-]Force extra property checks if object is explicitly casted or at-least give us an exact type[/-][+]Force extra property checks if object is explicitly casted or at-least give us a StrictPropertyCheck/Exact type[/+]on Jul 24, 2019
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
interfaceIComponent<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;}classComponent<State>implementsIComponent<State>{publicstate: State;constructor(){this.state=Object.assign({}asState,this.config.defaultState);}publicgetconfig(){return{defaultState: {}asState,}}update(stateUpdate: Partial<State>){Object.assign(this.state,stateUpdate);}}interfaceSwitchState{on: boolean;}classSwitchComponentextendsComponent<SwitchState>{publicgetconfig(){return{defaultState: {on: false,blah: 123,// no error, ¯\_(ツ)_/¯ - dead code - I want TS to throw error for extra properties},}}}
Activity
[-]Force extra property checks if object is explicitly casted or at-least give us an exact type[/-][+]Force extra property checks if object is explicitly casted or at-least give us a StrictPropertyCheck/Exact type[/+]RyanCavanaugh commentedon Jul 24, 2019
Sounds like #7481, though it's hard to tell because the example doesn't require a type assertion.
nojvek commentedon Jul 24, 2019
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
RyanCavanaugh commentedon Jul 24, 2019
There's already a way to do that, though - put a return type annotation on
config().#7481 covers the pros and cons of various options here
nojvek commentedon Jul 24, 2019
Oh cool, that worked. The question is why is the type not inferred from the base interface. Why do I have to tell TS again ?
typescript-bot commentedon Jul 27, 2019
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.